Vue2学习笔记の使用Vue脚手架

hello, 各位小伙伴,这篇文章主要是Vue2学习笔记中的第三章:使用Vue脚手架,带领你从0搭建一个Vue脚手架!

使用Vue脚手架

脚手架文件结构

├── node_modules 
├── public
│   ├── favicon.ico: 页签图标
│   └── index.html: 主页面
├── src
│   ├── assets: 存放静态资源
│   │   └── logo.png
│   │── component: 存放组件
│   │   └── HelloWorld.vue
│   │── App.vue: 汇总所有组件
│   │── main.js: 入口文件
├── .gitignore: git版本管制忽略的配置
├── babel.config.js: babel的配置文件
├── package.json: 应用包配置文件 
├── README.md: 应用描述文件
├── package-lock.json:包版本控制文件

关于不同版本的Vue

  1. vue.js与vue.runtime.xxx.js的区别:
    1. vue.js是完整版的Vue,包含:核心功能 + 模板解析器。
    2. vue.runtime.xxx.js是运行版的Vue,只包含:核心功能;没有模板解析器。
  2. 因为vue.runtime.xxx.js没有模板解析器,所以不能使用template这个配置项,需要使用render函数接收到的createElement函数去指定具体内容。

vue.config.js配置文件

  1. 使用vue inspect > output.js可以查看到Vue脚手架的默认配置。
  2. 使用vue.config.js可以对脚手架进行个性化定制,详情见:https://cli.vuejs.org/zh

ref属性

1.被用来给元素或子组件注册引用信息(id的替代者)
2. 应用在html标签上获取的是真实DOM元素,应用在组件标签上是组件实例对象(vc)
3. 使用方式:

  1. 打标识:<h1 ref="xxx">.....</h1><School ref="xxx"></School>
  2. 获取:this.$refs.xxx

props配置项

1.功能:让组件接收外部传过来的数据

2.传递数据:<Demo name="xxx"/>

3.接收数据:

  1. 第一种方式(只接收):props:['name']

  2. 第二种方式(限制类型):props:{name:String}

  3. 第三种方式(限制类型、限制必要性、指定默认值):

    props:{
          
          
    	name:{
          
          
    	type:String, //类型
    	required:true, //必要性
    	default:'老王' //默认值
    	}
    }
    

备注:props是只读的,Vue底层会监测你对props的修改,如果进行了修改,就会发出警告,若业务需求确实需要修改,那么请复制props的内容到data中一份,然后去修改data中的数据。

Student.vue

<template>
	<div>
		<h1>{
   
   {msg}}</h1>
		<h2>学生姓名:{
   
   {name}}</h2>
		<h2>学生性别:{
   
   {sex}}</h2>
		<h2>学生年龄:{
   
   {myAge+1}}</h2>
		<button @click="updateAge">尝试修改收到的年龄</button>
	</div>
</template>

<script>
	export default {
      
      
		name:'Student',
		data() {
      
      
			console.log(this)
			return {
      
      
				msg:'我是一个尚硅谷的学生',
				myAge:this.age
			}
		},
		methods: {
      
      
			updateAge(){
      
      
				this.myAge++
			}
		},
		//简单声明接收
		// props:['name','age','sex'] 

		//接收的同时对数据进行类型限制
		/* props:{
			name:String,
			age:Number,
			sex:String
		} */

		//接收的同时对数据:进行类型限制+默认值的指定+必要性的限制
		props:{
      
      
			name:{
      
      
				type:String, //name的类型是字符串
				required:true, //name是必要的
			},
			age:{
      
      
				type:Number,
				default:99 //默认值
			},
			sex:{
      
      
				type:String,
				required:true
			}
		}
	}
</script>

App.vue

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

<script>
	import Student from './components/Student'

	export default {
      
      
		name:'App',
		components:{
      
      Student}
	}
</script>

mixin(混入)

1.功能:可以把多个组件共用的配置提取成一个混入对象

2.使用方式:

第一步定义混合:

{
    data(){....},
    methods:{....}
    ....
}

第二步使用混入:

全局混入:```Vue.mixin(xxx)```
局部混入:```mixins:['xxx']	```

main.js

//引入Vue
import Vue from 'vue'
//引入App
import App from './App.vue'
import {
    
    hunhe,hunhe2} from './mixin'
//关闭Vue的生产提示
Vue.config.productionTip = false

Vue.mixin(hunhe)
Vue.mixin(hunhe2)


//创建vm
new Vue({
    
    
	el:'#app',
	render: h => h(App)
})

mixin.js

export const hunhe = {
    
    
	methods: {
    
    
		showName(){
    
    
			alert(this.name)
		}
	},
	mounted() {
    
    
		console.log('你好啊!')
	},
}
export const hunhe2 = {
    
    
	data() {
    
    
		return {
    
    
			x:100,
			y:200
		}
	},
}

插件

1.功能:用于增强Vue

2.本质:包含install方法的一个对象,install的第一个参数是Vue,第二个以后的参数是插件使用者传递的数据。

3.定义插件:

对象.install = function (Vue, options) {
    
    
    // 1. 添加全局过滤器
    Vue.filter(....)

    // 2. 添加全局指令
    Vue.directive(....)

    // 3. 配置全局混入(合)
    Vue.mixin(....)

    // 4. 添加实例方法
    Vue.prototype.$myMethod = function () {
    
    ...}
    Vue.prototype.$myProperty = xxxx
}

4.使用插件:Vue.use()

scoped样式

1.作用:让样式在局部生效,防止冲突。
2.写法:<style scoped>

总结TodoList案例

1.组件化编码流程:
(1).拆分静态组件:组件要按照功能点拆分,命名不要与html元素冲突。
(2).实现动态组件:考虑好数据的存放位置,数据是一个组件在用,还是一些组件在用:
1).一个组件在用:放在组件自身即可。
2). 一些组件在用:放在他们共同的父组件上(<span style="color:red">状态提升</span>)。
(3).实现交互:从绑定事件开始。

2.props适用于:
(1).父组件 ==> 子组件 通信
(2).子组件 ==> 父组件 通信(要求父先给子一个函数)

3.使用v-model时要切记:v-model绑定的值不能是props传过来的值,因为props是不可以修改的!

4.props传过来的若是对象类型的值,修改对象中的属性时Vue不会报错,但不推荐这样做。

webStorage

1.存储内容大小一般支持5MB左右(不同浏览器可能还不一样)

2.浏览器端通过 Window.sessionStorage 和 Window.localStorage 属性来实现本地存储机制。

3.相关API:

1.xxxxxStorage.setItem('key', 'value');
该方法接受一个键和值作为参数,会把键值对添加到存储中,如果键名存在,则更新其对应的值。

2.xxxxxStorage.getItem('person');
该方法接受一个键名作为参数,返回键名对应的值。

3.xxxxxStorage.removeItem('key');
该方法接受一个键名作为参数,并把该键名从存储中删除。

4.xxxxxStorage.clear()
该方法会清空存储中的所有数据。

4.备注:

  1. SessionStorage存储的内容会随着浏览器窗口关闭而消失。
  2. LocalStorage存储的内容,需要手动清除才会消失。
  3. xxxxxStorage.getItem(xxx)如果xxx对应的value获取不到,那么getItem的返回值是null。
  4. JSON.parse(null)的结果依然是null。

组件的自定义事件

1.一种组件间通信的方式,适用于:<strong style="color:red">子组件 ===> 父组件</strong>

2.使用场景:A是父组件,B是子组件,B想给A传数据,那么就要在A中给B绑定自定义事件(<span style="color:red">事件的回调在A中</span>)。

3.绑定自定义事件:

  1. 第一种方式,在父组件中:<Demo @atguigu="test"/><Demo v-on:atguigu="test"/>

  2. 第二种方式,在父组件中:

    <Demo ref="demo"/>
    ......
    mounted(){
          
          
       this.$refs.xxx.$on('atguigu',this.test)
    }
    
  3. 若想让自定义事件只能触发一次,可以使用once修饰符,或$once方法。

4.触发自定义事件:this.$emit('atguigu',数据)

5.解绑自定义事件this.$off('atguigu')

6.组件上也可以绑定原生DOM事件,需要使用native修饰符。

7.注意:通过this.$refs.xxx.$on('atguigu',回调)绑定自定义事件时,回调要么配置在methods中,要么用箭头函数,否则this指向会出问题!

App.vue

<template>
	<div class="app">
		<h1>{
   
   {msg}},学生姓名是:{
   
   {studentName}}</h1>

		<!-- 通过父组件给子组件传递函数类型的props实现:子给父传递数据 -->
		<School :getSchoolName="getSchoolName"/>

		<!-- 通过父组件给子组件绑定一个自定义事件实现:子给父传递数据(第一种写法,使用@或v-on) -->
		<!-- <Student @atguigu="getStudentName" @demo="m1"/> -->

		<!-- 通过父组件给子组件绑定一个自定义事件实现:子给父传递数据(第二种写法,使用ref) -->
		<Student ref="student" @click.native="show"/>
	</div>
</template>

<script>
	import Student from './components/Student'
	import School from './components/School'

	export default {
      
      
		name:'App',
		components:{
      
      School,Student},
		data() {
      
      
			return {
      
      
				msg:'你好啊!',
				studentName:''
			}
		},
		methods: {
      
      
			getSchoolName(name){
      
      
				console.log('App收到了学校名:',name)
			},
			getStudentName(name,...params){
      
      
				console.log('App收到了学生名:',name,params)
				this.studentName = name
			},
			m1(){
      
      
				console.log('demo事件被触发了!')
			},
			show(){
      
      
				alert(123)
			}
		},
		mounted() {
      
      
			this.$refs.student.$on('atguigu',this.getStudentName) //绑定自定义事件
			// this.$refs.student.$once('atguigu',this.getStudentName) //绑定自定义事件(一次性)
		},
	}
</script>

<style scoped>
	.app{
      
      
		background-color: gray;
		padding: 5px;
	}
</style>

Student.vue

<template>
	<div class="student">
		<h2>学生姓名:{
   
   {name}}</h2>
		<h2>学生性别:{
   
   {sex}}</h2>
		<h2>当前求和为:{
   
   {number}}</h2>
		<button @click="add">点我number++</button>
		<button @click="sendStudentlName">把学生名给App</button>
		<button @click="unbind">解绑atguigu事件</button>
		<button @click="death">销毁当前Student组件的实例(vc)</button>
	</div>
</template>

<script>
	export default {
      
      
		name:'Student',
		data() {
      
      
			return {
      
      
				name:'张三',
				sex:'男',
				number:0
			}
		},
		methods: {
      
      
			add(){
      
      
				console.log('add回调被调用了')
				this.number++
			},
			sendStudentlName(){
      
      
				//触发Student组件实例身上的atguigu事件
				this.$emit('atguigu',this.name,666,888,900)
				// this.$emit('demo')
				// this.$emit('click')
			},
			unbind(){
      
      
				this.$off('atguigu') //解绑一个自定义事件
				// this.$off(['atguigu','demo']) //解绑多个自定义事件
				// this.$off() //解绑所有的自定义事件
			},
			death(){
      
      
				this.$destroy() //销毁了当前Student组件的实例,销毁后所有Student实例的自定义事件全都不奏效。
			}
		},
	}
</script>

<style lang="less" scoped>
	.student{
      
      
		background-color: pink;
		padding: 5px;
		margin-top: 30px;
	}
</style>

School.vue

<template>
	<div class="school">
		<h2>学校名称:{
   
   {name}}</h2>
		<h2>学校地址:{
   
   {address}}</h2>
		<button @click="sendSchoolName">把学校名给App</button>
	</div>
</template>

<script>
	export default {
      
      
		name:'School',
		props:['getSchoolName'],
		data() {
      
      
			return {
      
      
				name:'尚硅谷',
				address:'北京',
			}
		},
		methods: {
      
      
			sendSchoolName(){
      
      
				this.getSchoolName(this.name)
			}
		},
	}
</script>

<style scoped>
	.school{
      
      
		background-color: skyblue;
		padding: 5px;
	}
</style>

全局事件总线(GlobalEventBus)

1.一种组件间通信的方式,适用于<span style="color:red">任意组件间通信</span>

2.安装全局事件总线:

new Vue({
    
    
	......
	beforeCreate() {
    
    
		Vue.prototype.$bus = this //安装全局事件总线,$bus就是当前应用的vm
	},
    ......
}) 

3.使用事件总线:

  1. 接收数据:A组件想接收数据,则在A组件中给$bus绑定自定义事件,事件的回调留在A组件自身。

    methods(){
          
          
      demo(data){
          
          ......}
    }
    ......
    mounted() {
          
          
      this.$bus.$on('xxxx',this.demo)
    }
    
  2. 提供数据:this.$bus.$emit('xxxx',数据)

4.最好在beforeDestroy钩子中,用$off去解绑<span style="color:red">当前组件所用到的</span>事件。

接受方:

<template>
	<div class="school">
		<h2>学校名称:{
   
   {name}}</h2>
		<h2>学校地址:{
   
   {address}}</h2>
	</div>
</template>

<script>
	export default {
      
      
		name:'School',
		data() {
      
      
			return {
      
      
				name:'尚硅谷',
				address:'北京',
			}
		},
		mounted() {
      
      
			// console.log('School',this)
			this.$bus.$on('hello',(data)=>{
      
      
				console.log('我是School组件,收到了数据',data)
			})
		},
		beforeDestroy() {
      
      
			this.$bus.$off('hello')
		},
	}
</script>

<style scoped>
	.school{
      
      
		background-color: skyblue;
		padding: 5px;
	}
</style>

传数方:

<template>
	<div class="student">
		<h2>学生姓名:{
   
   {name}}</h2>
		<h2>学生性别:{
   
   {sex}}</h2>
		<button @click="sendStudentName">把学生名给School组件</button>
	</div>
</template>

<script>
	export default {
      
      
		name:'Student',
		data() {
      
      
			return {
      
      
				name:'张三',
				sex:'男',
			}
		},
		mounted() {
      
      
			// console.log('Student',this.x)
		},
		methods: {
      
      
			sendStudentName(){
      
      
				this.$bus.$emit('hello',this.name)
			}
		},
	}
</script>

<style lang="less" scoped>
	.student{
      
      
		background-color: pink;
		padding: 5px;
		margin-top: 30px;
	}
</style>

消息订阅与发布(pubsub)

1.一种组件间通信的方式,适用于<span style="color:red">任意组件间通信</span>

2.使用步骤:

  1. 安装pubsub:npm i pubsub-js

  2. 引入: import pubsub from 'pubsub-js'

3.接收数据:A组件想接收数据,则在A组件中订阅消息,订阅的回调留在A组件自身。

 ```javascript
  methods(){
    demo(data){......}
  }
  ......
  mounted() {
    this.pid = pubsub.subscribe('xxx',this.demo) //订阅消息
  }
  ```

4.提供数据:pubsub.publish('xxx',数据)

5.最好在beforeDestroy钩子中,用PubSub.unsubscribe(pid)去取消订阅。

接收方:

<template>
	<div class="school">
		<h2>学校名称:{
   
   {name}}</h2>
		<h2>学校地址:{
   
   {address}}</h2>
	</div>
</template>

<script>
	import pubsub from 'pubsub-js'
	export default {
      
      
		name:'School',
		data() {
      
      
			return {
      
      
				name:'尚硅谷',
				address:'北京',
			}
		},
		mounted() {
      
      
			// console.log('School',this)
			/* this.$bus.$on('hello',(data)=>{
				console.log('我是School组件,收到了数据',data)
			}) */
			this.pubId = pubsub.subscribe('hello',(msgName,data)=>{
      
      
				console.log(this)
				// console.log('有人发布了hello消息,hello消息的回调执行了',msgName,data)
			})
		},
		beforeDestroy() {
      
      
			// this.$bus.$off('hello')
			pubsub.unsubscribe(this.pubId)
		},
	}
</script>

<style scoped>
	.school{
      
      
		background-color: skyblue;
		padding: 5px;
	}
</style>

传数方:

<template>
	<div class="student">
		<h2>学生姓名:{
   
   {name}}</h2>
		<h2>学生性别:{
   
   {sex}}</h2>
		<button @click="sendStudentName">把学生名给School组件</button>
	</div>
</template>

<script>
	import pubsub from 'pubsub-js'
	export default {
      
      
		name:'Student',
		data() {
      
      
			return {
      
      
				name:'张三',
				sex:'男',
			}
		},
		mounted() {
      
      
			// console.log('Student',this.x)
		},
		methods: {
      
      
			sendStudentName(){
      
      
				// this.$bus.$emit('hello',this.name)
				pubsub.publish('hello',666)
			}
		},
	}
</script>

<style lang="less" scoped>
	.student{
      
      
		background-color: pink;
		padding: 5px;
		margin-top: 30px;
	}
</style>

nextTick

1.语法:this.$nextTick(回调函数)
2.作用:在下一次 DOM 更新结束后执行其指定的回调。
3.什么时候用:当改变数据后,要基于更新后的新DOM进行某些操作时,要在nextTick所指定的回调函数中执行。

Vue封装的过度与动画

1.作用:在插入、更新或移除 DOM元素时,在合适的时候给元素添加样式类名。

2.图示:
在这里插入图片描述

3.写法:

  1. 准备好样式:

    元素进入的样式:

    1. v-enter:进入的起点
    2. v-enter-active:进入过程中
    3. v-enter-to:进入的终点

    元素离开的样式:

    1. v-leave:离开的起点
    2. v-leave-active:离开过程中
    3. v-leave-to:离开的终点
  2. 使用<transition>包裹要过度的元素,并配置name属性:

    <transition name="hello">
    	<h1 v-show="isShow">你好啊!</h1>
    </transition>
    
  3. 备注:若有多个元素需要过度,则需要使用:<transition-group>,且每个元素都要指定key值。

方式一使用动画的方式:

<template>
	<div>
		<button @click="isShow = !isShow">显示/隐藏</button>
		<transition name="hello" appear>
			<h1 v-show="isShow">你好啊!</h1>
		</transition>
	</div>
</template>

<script>
	export default {
      
      
		name:'Test',
		data() {
      
      
			return {
      
      
				isShow:true
			}
		},
	}
</script>

<style scoped>
	h1{
      
      
		background-color: orange;
	}

	.hello-enter-active{
      
      
		animation: atguigu 0.5s linear;
	}

	.hello-leave-active{
      
      
		animation: atguigu 0.5s linear reverse;
	}

	@keyframes atguigu {
      
      
		from{
      
      
			transform: translateX(-100%);
		}
		to{
      
      
			transform: translateX(0px);
		}
	}
</style>

方法二使用过渡的方式:

<template>
	<div>
		<button @click="isShow = !isShow">显示/隐藏</button>
		<transition-group name="hello" appear>
			<h1 v-show="!isShow" key="1">你好啊!</h1>
			<h1 v-show="isShow" key="2">尚硅谷!</h1>
		</transition-group>
	</div>
</template>

<script>
	export default {
      
      
		name:'Test',
		data() {
      
      
			return {
      
      
				isShow:true
			}
		},
	}
</script>

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

</style>

多个元素的过渡:

<template>
	<div>
		<button @click="isShow = !isShow">显示/隐藏</button>
		<transition-group 
			appear
			name="animate__animated animate__bounce" 
			enter-active-class="animate__swing"
			leave-active-class="animate__backOutUp"
		>
			<h1 v-show="!isShow" key="1">你好啊!</h1>
			<h1 v-show="isShow" key="2">尚硅谷!</h1>
		</transition-group>
	</div>
</template>

<script>
	import 'animate.css'
	export default {
      
      
		name:'Test',
		data() {
      
      
			return {
      
      
				isShow:true
			}
		},
	}
</script>

<style scoped>
	h1{
      
      
		background-color: orange;
	}
	

</style>

(注意:以上的部分的.vue文件由于没有对应的.vue代码模板,代码由于无法高亮,所以选择了html格式)

猜你喜欢

转载自blog.csdn.net/weixin_51735748/article/details/132449268
今日推荐