Vue的脚手架

2023-12-19

脚手架配置

脚手架文档: Vue CLI

npm config set registry https://registry.npm.taobao.org

vue.config.js配置选项:

配置参考 | Vue CLI

ref选项

ref和id类似,给标签打标识。

document.getElementById('btn');

this.$ref.btn;

父子组件间通信

App.vue 加上冒号之后,18就成了一个表达式。 传递到Student中,就可以进行运算了。


<Student name="李四" age="18"/>
<Student name="李四" :age="18"/>    

Student.vue,接受的数据,相当于跑到了VC中的data里边。


props:['name','age']
props:{
    name:String,
    age:Number,
    sex:String
}
props:{
    name:{
        type:String,
        required:true,
        default:''
    }
}  

props收到的属性,是不建议修改的,控制台会报错的。


data(){
    return{
        myAge:this.age
    }
}  

但是将收到的属性值,赋值到新的字段中,是可以支持修改的。

引入混合js[复用配置]


mixin.js
export const hunhe = {
    methods:{
        showName(){
            //....
        }
    },
    mounted(){
        //....
    }
}  

引入


import {hunhe} from '../mixin'
export default{
    name:'Student',
    data(){
        return{
        
        }
    },
    mixins:[hunhe],
}  

全局混合,不建议用


import {hunhe,hunhe2} from './mixin'
Vue.mixin(hunhe)  

插件plugins


plugins.js
export default{
    install(Vue){
        console.log('aaa');
        Vue.filter(...)
    }
}
main.js 在new Vue({})上边引入
import plugins from './plugins'
Vue.use(plugins)  

scoped作用域


<style lang="less" scoped>
....
</style>  

查看npm库版本


npm view webpack versions

npm i less-loader@7  

子传父组件通信-v1


App.vue
methods:{
	receive(x){
		
	}
}
<Myheader :receive="receive"/>
//==============================================================
MyHeader.vue
props:['receive'],
methods:{
	add(e){
		this.reveive(e.target.value);
	}
}  

统计数量


computed:{
	doneTotal(){
		let i = 0;
		this.todos.forEach((todo)=>{
			if(todo.done) i++;
		})
		return i;
	}
}  

const x = this.todus.reduce((pre,current)=>{
	return pre+(current.todo ? 1: 0);
},0); //0是传入的初始值 {}里边的逻辑,数组有多少个,就调用多少次  
第二次调用时,pre就是第一次调用函数的返回值
current就是每一个todo项
x就是计算todo项为true的统计
  

computed:{
	doneTotal(){
		return this.todos.reduce((pre,todo)=>pre+(todo.done?1:0),0);
	}
}  

浏览器的本地存储

localstorage也可以用在移动端开发中

**组件的自定义事件通信**

1、通过父组件给子组件传递函数类型的props实现,子给父传递数据

2、绑定自定义事件传递给父组件


//自定义事件,给Student所在的vc绑定事件
App.vue
<Student v-on:pshdhx="demo"/> 
methods:{
	demo(name){
		console.log('demo被调用了',name)
	}
}
Student.vue
<button @click="sendStudentName">把学生名传递给app</button>
methods:{
	sendStudentName(){
		this.$emit('pshdhx',this.name) //触发Student组件实例的pshdhx事件
	}
}
  

3、使用ref来替换


//使用ref来替换  <Student v-on:pshdhx="demo"/>  <Student @pshdhx.once="demo"/>
App.vue
<Student ref="student"/> 
methods:{
	getStudentName(name,...params){ //params是一个数组
		console.log('App.vue收到了Student.vue传递过来的name值',name,params);
	}
}
mounted:{
	this.$refs.student.$on('pshdhx',this.getStudentName);
	this.$refs.student.$once('pshdhx',this.getStudentName);
}  

解绑自定义事件


//方法区域
unbind(){
	this.$off('pshdhx'); //只适用于解绑一个
	this.$off(['pshdhx','demo2']); //用数组来解绑多个自定义事件
	this.$off();//解绑所有
}  

箭头函数没有自己的this,所以就往外找。

自定义组件要想使用Vue事件


<Student @click.native="showInfo"/>
//如果不加.native,它就是一个自定义事件。  

全局事件总线

任意组件之间的通信。

傀儡VueComponent,就是x


//App.vue
const Demo = Vue.extend({})
const d = new Demo();
Vue.prototype.x = d;

//School.vue
mounted(){
	this.x.$on('hello',(data)=>{
		console.log('我是school组件,收到了数据',data);
	})
}
//Student.vue
methods:{
	sendStudentName(){
		this.x.$emit('hello',666);
	}
}  

构建傀儡组件2 ,就是x


new Vue({
	el:'#app',
	render:h=>h(App),
	beforeCreate(){
		Vue.prototype.x = this
	}
})  

x就是$bus了。

$bus很累了,所以销毁组件的时候,就要关闭


beforeDestory(){
	this.$bus.off('hello');
}  

消息订阅与发布

npm i pubsub-js


School.vue
import pubsub from 'pubsub-js'
mounted:{
	this.pubid = pubsub.subscribe('hello',function(name,data){
		console.log('有人发布了hello消息,hello的回调执行了',data)
	})
}
Student.vue
import pubsub from 'pubsub-js'
methods:{
	sendStudentName(){
		pubsub.publish('hello',666);
	}
}  

取消订阅,订阅之后,返回了一个id,需要销毁。


beforeDestory(){
	pubsub.unsubscribe(this.pubid);
}  

判断对象有没有这个属性

todo.hasOwnProperty('isEdit')

$nextTick

点击编辑按钮,input获取焦点:


<input ref="inputTitle"/>
this.$ref.inputTitle.focus(); settimeout
this.$nextTick(function(){
	//会在dom节点更新之后,才会触发
})  

动画效果

<template>
    <div>
        <button @click="isShow = !isShow">显示/隐藏</button>
<!--        <transition name="test" :appear="true"> 刷新时,也能有动画效果 -->
        <transition name="test" appear>
            <h1 v-show="isShow" class="come">你好呀</h1>
        </transition>
    </div>
</template>

<script>
    export default {
        // eslint-disable-next-line vue/multi-word-component-names
        name: "Test",
        data(){
            return{
                isShow:true
            }
        }
    }
</script>

<style scoped>

    /*借助动画效果去实现过度*/

    h1{
        background-color: aqua;
    }
    /*入场动画 名称固定 配合transition 使用*/
    /*.v-enter-active{*/
    .test-enter-active{
        animation: pshdhx 1s ;
    }

    /*离场动画,名称固定*/
    /*.v-leave-active{*/
    .test-leave-active{
        animation: pshdhx 1s reverse;
    }

    /*定义动画*/
    @keyframes pshdhx {
        from{
            transform: translateX(-100%);
        }
        to{
            transform: translateX(0px);
        }
    }
</style>
<template>
    <div>
        <button @click="isShow = !isShow">显示/隐藏2</button>
<!--        <transition name="test" :appear="true"> 刷新时,也能有动画效果 -->
        <transition name="test2" appear>
            <h1 v-show="isShow" class="come">你好呀2</h1>
        </transition>
    </div>
</template>

<script>
    export default {
        // eslint-disable-next-line vue/multi-word-component-names
        name: "Test2",
        data(){
            return{
                isShow:true
            }
        }
    }
</script>

<style scoped>
    /*接触过度效果去实现过度*/

    h1{
        background-color: aqua;
    }
    /*进入的起点、离开的终点*/
    .test2-enter,.test2-leave-to{
        transform: translateX(-100%);
    }
    .test2-enter-active,.test2-leave-active{
        transition: 0.5s linear;
    }
    /*进入的终点,离开的起点*/
    .test2-enter-to,.test2-leave{
        transform: translateX(0);
    }

</style>
<template>
    <div>
        <button @click="isShow = !isShow">显示/隐藏3</button>
        <transition appear
                    name="test"
                    enter-active-class="animate__animated animate__swing"
                    leave-active-class="animate__animated animate__backOutUp"
        >
            <h1 v-if="isShow">你好呀3</h1>
        </transition>
    </div>
</template>

<script>
    import 'animate.css';
    export default {
        // eslint-disable-next-line vue/multi-word-component-names
        name: "Test3",
        data() {
            return {
                isShow: true
            }
        }
    }
</script>

<style scoped>
    /*接触过度效果去实现过度*/

    h1 {
        background-color: antiquewhite;
    }


</style>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Vue的脚手架 的相关文章

随机推荐

  • android系统镜像文件

    boot img 这是包含内核和设备树 Device Tree 的镜像文件 它被引导加载程序 bootloader 加载以启动系统 并负责将控制权转交给内核 dtbo img 这是设备树增量编译 Device Tree Overlay 的镜
  • Android Uri scheme协议file转content

    一 Uri的介绍 在Android开发中 Uri Uniform Resource Identifier 是用于标识和访问各种资源的核心概念 这些资源可能包括文件 网络URL 数据库记录等 在处理这些资源时 我们可能会遇到不同的Uri协议
  • Python中使用JsonPath:概念、使用方法与案例

    目录 一 引言 二 JsonPath的概念 三 JsonPath的使用方法 1 安装JsonPath库 2 使用JsonPath查询JSON数据 3 复杂的JsonPath查询表达式 四 JsonPath的使用技巧和最佳实践 1 简化Jso
  • Template for directive ‘electronicSign‘ must have exactly one root element.

    问题 原因 angularjs 提示模版指令需要根元素 解决办法 清理头尾
  • Object.defineProperty

    直接在一个对象上定义一个新属性 或者修改一个已经存在的属性 Object defineProperty obj prop desc let person name 码农 age 18 Object defineProperty person
  • 【Unity】运行时创建曲线(贝塞尔的运用)

    Unity 运行时创建线 贝塞尔的运用 1 实现的目标 在运行状态下创建一条可以使用贝塞尔方法实时编辑的网格曲线 2 原理介绍 2 1 曲线的创建 unity建立网格曲线可以参考 Unity程序化网格体 的实现方法 主要分为顶点 三角面 U
  • #HarmonyOS:项目结构图

    hvigor 存储构建配置文件信息 idea 存储项目的配置信息 AppScope 全局的共有资源存放目录
  • 【JavaScript】Set方法

    基本用法 ES6 提供了新的数据结构 Set 它类似于数组 但是成员的值都是唯一的 没有重复的值 Set 本身是一个构造函数 用来生成 Set 数据结构 const s new Set 2 3 5 4 5 2 2 forEach x gt
  • 模具企业MES管理系统需求特点分析

    模具制造行业是制造业中的重要领域 为各种行业提供着模具和工装的制造与加工服务 在这个竞争激烈的行业中 企业需要高效管理和控制生产过程 以满足客户需求 提高产品质量和降低成本 MES生产管理系统作为一种集成的信息系统 为企业提供了实时的生产数
  • C++接口类使用Qt的虚信号

    项目中封装库的对外接口类 包括Qt的插件框架 希望接口类是一个比较干净的类 不需要继承自Object 与Qt无关系 但又需要它的子类使用Qt的信号和槽机制 则可以如下处理 接口类 处理请求 pragma once include thnet
  • 一文讲清楚内外网数据交换方案 可以解决哪些问题?

    首先 有内外网数据交换方案这个需求 前提是一定是做了内外网的隔离 将核心数据保护在内网之中 不能随意进行传输使用 做内外网隔离 在企业以及一些监管机构里面 比如金融 电力等行业 还是很普遍的的 一方面是出于自身安全性考虑 一方面是处于监管要
  • 面试题:偏向锁的十连问,你能接住几个?

    文章目录 前言 名词解释 问题解析 问题1 如何判断当前锁对象为偏向锁 问题2 偏向锁如何判断锁重入 问题3 符合什么条件才会尝试获取偏向锁 问题4 线程进入偏向锁后 会不会创建lock record 问题5 偏向锁膨胀后
  • npm run build Last few GCs

    npm run build Last few GCs 这是由于webpack执行时造成的内存溢出 lt Last few GCs gt 3906 0x3ce6a70 165833 ms Mark sweep 1374 0 1425 0 gt
  • Cannot find module ‘xxx‘

    方法一 直接进行npm install重新打包 方法二 如果npm install重新打包之后 仍然出现这个问题 可以进行删除node modules文件夹 同时强制清除缓存 rm rf node modules npm cache cle
  • C 库函数 - gmtime()

    描述 C 库函数 struct tm gmtime const time t timer 使用 timer 的值来填充 tm 结构 并用协调世界时 UTC 也被称为格林尼治标准时间 GMT 表示 声明 下面是 gmtime 函数的声明 st
  • 【QT】解决QTableView修改合并单元格内容无法修改到合并范围内的单元格

    问题 修改合并单元格的内容 修改合并单元格的内容时 希望直接修改到合并范围内的单元格 Qt没有实现这个功能 需要自己写出 Delegate来实现 方案 Delegate class EditDelegate public QStyledIt
  • 新年跨年烟花超酷炫合集【内含十八个烟花酷炫效果源码】

    以下展示为全部烟花特效效果 下方仅展示部分代码 源码获取见文末 HTML5烟花喷泉
  • 1005. K 次取反后最大化的数组和 && 增强for循环(foreach循环)遍历数组

    1005 K 次取反后最大化的数组和 原题链接 完成情况 解题思路 参考代码 1005K次取反后最大化的数组和 1005K次取反后最大化的数组和 简洁写法 错误经验吸取 增强for循环 foreach循环 遍历数
  • 关注MCU 开发中的无限循环

    在 MCU 的开发过程中 我们经常会遇到需要使用无限循环的情况 例如 在前后台系统中 我们需要在一个无限循环中处理各种任务 在实时性操作系统中 我们也可能需要在一个无限循环中调度各个任务 那么 处理无限循环的语句有哪些写法呢 目前常见的有两
  • Vue的脚手架

    脚手架配置 脚手架文档 Vue CLI npm config set registry https registry npm taobao org vue config js配置选项 配置参考 Vue CLI ref选项 ref和id类似