Vue.js 2使用中的难点举例--子组件,slot, 动态组件,事件监听
2017-04-04 17:59
1176 查看
一例打尽。。:)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="demo.css" /> </head> <body> <div id="app"> <ul> <li @click="currentView = 'home'" >Home</li> <li @click="currentView = 'list'" >List</li> <li @click="currentView = 'detail'" >Detail</li> </ul> <keep-alive> <component :is="currentView"></component> </keep-alive> <br/> <component :is="currentView"></component> <br/><br/> <my-slot> <p slot="title"> {{title}}</p> <div slot="content"> {{ content}} </div> </my-slot> <br/><br/> <comp-a></comp-a> <comp-b></comp-b> </div> </body> <script src="http://cdn.bootcss.com/vue/2.2.4/vue.min.js"></script> <script> Vue.component('my-slot', { template: '<div> \ <div class="title"> \ <slot name="title"></slot> \ </div> \ <div class="content"> \ <slot name="content"></slot> \ </div> \ </div>' }) var bus = new Vue(); var vm = new Vue({ el: "#app", data: { currentView: 'home', title: "This is a title", content: "This is the content" }, components: { home: { template: '<div> \ <p>Home</p> \ <ul> \ <li v-for="item in items"> {{ item }} </li> \ </ul> \ </div>', data: function() { return { items: [] } }, active: function(done) { var that = this; setTimeout(function() { that.items = [1, 2, 3, 4, 5]; done(); }, 1000) } }, list: { template: '<div>List</div>' }, detail: { template: '<div>Detail</div>' }, compA: { template: '<div> \ <input type="text" v-model="name" /> \ <button @click="create">增加</button> \ </div>', data: function() { return { a: "" } }, methods: { create: function() { bus.$emit('create', {name: this.name}); this.name=''; } } }, compB: { template: '<ul> \ <li v-for="item in items">{{ item.name }} </li> \ </ul>', data: function() { return { items: [] } }, mounted() { var that = this; bus.$on('create', function(data){ that.items.push(data); }) } } } }) </script> </html>
相关文章推荐
- 使用vue.js在页面内组件监听scroll事件的方法
- 使用vue.js在页面内组件监听scroll事件
- vue.js通过子组件通信和父子组件通信实现动态表单动态绑定事件(适合交互系统统一使用)
- vue使用$emit时,父组件无法监听到子组件的事件
- 常用js框架之vue.js(深入四:组件2,slot内容分发,编译作用域,动态切换组件)
- vue使用$emit时,父组件无法监听到子组件的事件实例
- Vue.js实战之通过监听滚动事件实现动态锚点
- Select中使用js动态修改了Option时如何触发onchange事件
- 监听浏览器关闭按钮点击事件 -- 使用JS
- highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度
- Vue.js-----轻量高效的MVVM框架(十二、组件动态切换)
- php使用post动态选择头像和js事件动态改变头像
- 监听浏览器关闭按钮点击事件 -- 使用JS
- JS事件监听 JS:attachEvent和addEventListener 使用方法
- Vue.js动态组件解析
- Vue.js-----轻量高效的MVVM框架(使用slot分发内容)
- Vue.js-----轻量高效的MVVM框架(十一、使用slot分发内容)
- 使用JS监听键盘两个组合键触发的事件
- JS事件监听 JS:attachEvent和addEventListener 使用方法
- JS事件监听 JS:attachEvent和addEventListener 使用方法