全局组件 [代码实例]
<body> <div id="app"> <!-- <home-nav></home-nav> --> <HomeNav></HomeNav> <home-nav></home-nav> <product-item></product-item> <product-item></product-item> <product-item></product-item> </div> <template id="nav"> <h2>我是应用程序的导航</h2> </template> <template id="product"> <div class="product"> <h2>{{title}}</h2> <p>商品描述, 限时折扣, 赶紧抢购</p> <p>价格: {{price}}</p> <button @click="favarItem">收藏</button> </div> </template> <script src="../lib/vue.js"></script> <script> // 1.创建app const app = Vue.createApp({ // data: option api data() { return { message: "Hello Vue" } }, }) // 2.注册全局组件 app.component("product-item", { template: "#product", data() { return { title: "我是商品Item", price: 9.9 } }, methods: { favarItem() { console.log("收藏了当前的item") } } }) app.component("HomeNav", { template: "#nav" }) // 2.挂载app app.mount("#app") </script> </body>
本文来自博客园,作者:杨建鑫,转载请注明原文链接:https://www.cnblogs.com/qd-lbxx/p/16595203.html