vue2补充内容
父传子
$attrs传值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head>
<body> <div id="app"> <parent :msg="msg" hello="hello" color="color"></parent> </div> <script src="./lib/vue.js"></script> <script> let child = { template: ` <div>子组件</div> `, props: { hello: {
} }, created() { console.log(this.$attrs) }, } let parent = { template: ` <div>父组件<child v-bind="$attrs"></child></div> `, components: { child } } new Vue({ el: "#app", data: { msg: 'hello' }, components: { parent } }) </script> </body> </html>
|
字传父
$listener传事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| <!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head>
<body> <div id="app"> <parent @click="handler"></parent> </div> <script src="./lib/vue.js"></script> <script> Vue.config.productionTip = false let child = { template: ` <div>子组件<button @click="handler">传值</button></div> `, methods: { handler() { this.$emit('click', '传递的数据') } }, created() { console.log(this.$listeners) }, } let parent = { template: ` <div>父组件<child v-on="$listeners"></child></div> `, components: { child }, created(){ console.log(this.$listeners) } } new Vue({ el: "#app", data: { msg: 'hello' }, components: { parent }, methods:{ handler(v){ console.log(v); } } }) </script> </body>
</html>
|
混入 mixin
mixin相当于一个公共代码块,它其实也是一个对象,也就是说这个里面的内容是提供给其他组件使用的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| <!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head>
<body> <div id="app"> {{msg}} </div> <script src="./lib/vue.js"></script> <script> Vue.config.productionTip = false //mixin的内容设计和组件中的内容设计基本一致 let public_mixin = { data() { return { msg: 'hello' } }, //方法 methods: { sayhello() { console.log('hello') } }, //计算属性 computed: { sum() { return 1 + 0 } }, //钩子函数 created() { console.log('hello Vue is created!') }, //监听 watch: { msg() { console.log('msg changed') } } } // 全局混入mixin // Vue.mixin(public_mixin)//data中数据名冲突时,vue实例的data优先级更高
new Vue({ el: "#app", data: { message:'hello' }, // 局部混入mixin mixins:[public_mixin] }) </script> </body>
</html>
|
devserver配置
项目相关命令
1 2 3 4
| npm run serve 运行项目 npm run dev 启动开发服务 npm run lint 运行eslint检索格式化 npm run build 打包
|