首页 > 语言 > JavaScript > 正文

Vue 多层组件嵌套二种实现方式(测试实例)

2024-05-06 15:27:03
字体:
来源:转载
供稿:网友

最近在研究vue组件的学习,给自己留个笔记,也分享给大家,希望能对大家有所帮助

<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>Vue 测试实例-组件嵌套二种方式</title> <script src="https://cdn.bootcss.com/vue/2.1.10/vue.js"></script></head><body> <div id="app">  <Itemlist1>    <Item v-for="item in items1" :data="item"/>  </Itemlist1>  <Itemlist2 :itemlist="items2"></Itemlist2> </div> <script> Vue.component('Item',{  template: '<div>{{data.name}}</div>',  props: {    data:Object  } }); // 方式一:嵌套组件时用<slot></slot>, Vue.component("Itemlist1", {  template: '<div @click="ok"><slot></slot></div>',  props: {   itemList: Array  },  methods: {   ok: function() {    alert(this.abc);   }  } }); // 方式二: Vue.component("Itemlist2", {  template: '<div @click="ok"><Item v-for="item in itemlist" :data="item"/></div>',  props: {   itemlist: Array  },  methods: {   ok: function() {    alert(this.abc);   }  } }); // 创建根实例 var vueApp = new Vue({  el: '#app',  data: {   items1: [{    'name': "item1"   }, {    'name': "item2"   }, {    'name': "item3"   }],   items2: [{    'name': "item1-1"   }, {    'name': "item2-1"   }, {    'name': "item3-1"   }]  } }) </script></body></html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持错新站长站。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选