目录
Vue中的插槽问题
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Vue的数据模型</title> <script src="../js/vue.js"></script> </head> <body> <!-- 容器 --> <div id="app"> {{msg}} <hr /> <my-com></my-com> <hr /> <my-com> <!-- //添加给哪个插槽 --> <!-- 访问不到组件内的数据comMsg--> <template v-slot:default>aaaa--{{msg}}</template> <!-- //v-slot:slot2 缩写 #slot2 只有这样写才能传参 --> <template #slot2="scope"> bbbb--{{scope.comMsg}}--{{scope.title}}--{{scope.age}} </template> <!-- #slot2="{comMsg}" 解构得到comMsg --> <template #slot2="{comMsg}"> BBB--{{comMsg}} </template> </my-com> </div> <script> let myCom = { //注册的组件内一定是函数 data() { return { comMsg: "组件的msg", }; }, //如果设置为默认插槽,则都会被 我不好 这个值覆盖 //如果不想被覆盖,那就要 具名化 进行区分 template: ` <div> <slot name="default" >默认内容1</slot> <div>111111111</div> <div>222222222</div> <slot name="slot2" :comMsg='comMsg' title='这是title' :age="20">默认内容2</slot> </div>`, }; Vue.component("my-com", myCom); // 整个页面只能有一个Vue实例对象 let vm = new Vue({ el: "#app", data: { msg: "hello", }, methods: {}, }); </script> </body> </html>
Vue动态获取数组内容综合案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Vue的数据模型</title> <script src="../js/vue.js"></script> </head> <body> <!-- 容器 --> <!-- 用一个组件完成数据表格的渲染 数据:学生数组, 老师数组, 课程数组 --> <div id="app"> {{msg}} <hr /> <!-- //这样就写死了,需要动态获取多少行的数据 --> <!-- <thead> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>张三</td> <td>23</td> </tr> <tr> <td>2</td> <td>张张</td> <td>22</td> </tr> </tbody> --> 学生数组: <my-table :data="students"> <template #header> <th prop="id">编号</th> <th>学号</th> <th>年龄</th> </template> <template #body="scope"> <td>{{scope.row.id}}</td> <td>{{scope.row.name}}</td> <td>{{scope.row.age}}</td> </template> </my-table> <hr /> 老师数组: <my-table :data="teachers"> <template #header> <th prop="id">编号</th> <th>学号</th> <th>年龄</th> <th>性别</th> </template> <template #body="scope"> <td>{{scope.row.id}}</td> <td>{{scope.row.name}}</td> <td>{{scope.row.age}}</td> <td>{{scope.row.gender}}</td> </template> </my-table> <hr /> 课程数组: <my-table :data="courses"> <template #header> <th prop="id">编号</th> <th>名字</th> <th>描述</th> </template> <template #body="scope"> <td>{{scope.row.id}}</td> <td>{{scope.row.name}}</td> <td>{{scope.row.desc}}</td> </template> </my-table> </div> <script> Vue.component("my-table", { props: ["data"], data() { return {}; }, template: ` <table> <thead> <tr> <!-- //在不能写死的部分使用插槽 --> <slot name="header"></slot> </tr> </thead> <tbody> <tr v-for="(item,index) in data" :key="index"> <slot name="body" :row="item"></slot> </tr> </tbody> </table> `, }); // 整个页面只能有一个Vue实例对象 let vm = new Vue({ el: "#app", data: { msg: "hello", students: [ { id: 1, name: "张三", age: 23, }, { id: 2, name: "小明", age: 20, }, ], teachers: [ { id: 1, name: "小李", age: "30", gender: "male", }, { id: 2, name: "小红", age: "30", gender: "sex", }, { id: 3, name: "小清", age: "30", gender: "sex", }, ], courses: [ { id: 1, name: "html", desc: "超文本", }, { id: 2, name: "css", desc: "层叠样式", }, ], }, methods: {}, }); </script> </body> </html>
Vue中动态组件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Vue的数据模型</title> <script src="../js/vue.js"></script> </head> <body> <!-- 容器 --> <div id="app"> {{msg}} <hr /> <button @click="currentCom='com-a'">A组件</button> <button @click="currentCom='com-b'">B组件</button> <button @click="currentCom='com-c'">C组件</button> <div style="border: 2px solid red; width: 200px; height: 200px"> <!-- 静态is值 值为注册的组件的名称--> <!-- <component is="com-a"></component> --> <!-- 动态is值 值为绑定的一个变量名称--> <!-- //频繁组件切换,没有数据更新,可以用keep-alive标签组件缓存起来 --> <keep-alive> <component :is="currentCom"></component> </keep-alive> <!-- 不可行 --> <!-- <component :is="comC"></component> <component is="comC"></component> --> </div> </div> <script> let comA = { template: ` <div>A 组件的内容</div> `, created() { console.log("A组件的created"); }, }; let comB = { template: ` <div>B组件的内容</div> `, created() { console.log("B组件的created"); }, }; let comC = { template: ` <div>C组件的内容</div> `, created() { console.log("C组件的created"); }, }; Vue.component("com-a", comA); Vue.component("com-b", comB); Vue.component("com-c", comC); // 整个页面只能有一个Vue实例对象 let vm = new Vue({ el: "#app", data: { msg: "hello", currentCom: comC, }, methods: { clickBtn() {}, }, }); </script> </body> </html>
Vue中对象混入问题
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Vue的数据模型</title> <script src="../js/vue.js"></script> </head> <body> <!-- 容器 --> <div id="app"> {{msg}}--{{mixMsg}}--{{obj}} <br /> <button @click="test">按钮</button> </div> <script> // 声明一个混入对象 let mixin = { data() { return { mixMsg: "混入的msg", //优先使用组件中的方法 msg: "混入msg", obj: { name: "张三", age: 20, }, }; }, created() { console.log("混入的created"); }, methods: { test() { console.log("这是混入test方法"); }, }, }; /* //全局混入 Vue.mixin(mixin); */ //混入之后 //数据模型data当中的属性会递归合并,有属性重名时,组件数据优先 //声明周期合并,两个都会合并,两个都会执行,混入的钩子在组件自生钩子之前调用 //值为对象的选项,methods、components、directives合并成一个对象,如果两个键名重名,以组件对象的值为主 // 整个页面只能有一个Vue实例对象 let vm = new Vue({ el: "#app", // 局部混入 mixins: [mixin], data: { msg: "hello", }, created() { console.log("组件的created"); }, methods: { test() { console.log("组件test方法"); }, }, }); </script> </body> </html>
Vue中的指令
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Vue的数据模型</title> <script src="../js/vue.js"></script> </head> <body> <!-- 容器 --> <div id="app"> {{msg}} <input type="text" /> <br /> <input type="text" v-focus /> </div> <script> //声明指令 let myFocus = { bind(el, binding, vNode, oldNode) {}, inserted(el, binding, vNode, oldNode) { console.log(el); el.focus(); //聚焦元素 }, update(el, binding, vnNde, oldNode) {}, componentUpdated(el, binding, vNode, oldNode) {}, unbind(el, binding, vNode, oldNode) {}, }; //指令全局注册 Vue.directive("focus", myFocus); // 整个页面只能有一个Vue实例对象 let vm = new Vue({ el: "#app", data: { msg: "hello", }, methods: {}, }); </script> </body> </html>
哪个input框添加了v-focus 光标就定位到哪里
Vue中的插槽slot、动态组件、对象混入、指令、动态获取数组内容
最新推荐文章于 2024-09-02 20:20:48 发布