Vue-组件
组件
组件是可复用的 Vue 实例,主要用于开发中 具有相同特征不同数据的模块 把它集成为一个组件 供重复利用
注册的组件不要跟系统标签同名
2.局部组件
一个vm实例可以有多个局部组件,但是只能供当前vm实例使用
组件引用的三种方式
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 | new Vue({ el: '#app' , data: {}, methods: {}, components:{ Box:{ template:`<div> <h1>6666</h1> <p>hello</p> </div>` }, Nav1:{ template:`<div> <div>按钮1</div> <div>按钮2</div> <div>按钮3</div> <div>按钮4</div> </div>` }, MyBox:{ template:`<h1>666</h1>` } }, filters:{}, watch:{}, computed:{}, directives:{}, beforeCreate(){}, created(){}, beforeMount(){}, mounted(){}, beforeUpdate(){}, updated(){}, beforeDestroy(){}, destroyed(){} }) |
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 | <script> let a1 = { template: `<div> <h1>6666</h1> <p>hello</p> </div>` } let a2 = { template: `<div> <div>按钮1</div> <div>按钮2</div> <div>按钮3</div> <div>按钮4</div> </div>` } let a3 = { template: `<h1>666</h1>` } var vm = new Vue({ el: '#app' , data: {}, methods: {}, components: { //注册组件 Box: a1, Nav1: a2, MyBox: a3 }, filters: {}, watch: {}, computed: {}, directives: {}, beforeCreate() {}, created() {}, beforeMount() {}, mounted() {}, beforeUpdate() {}, updated() {}, beforeDestroy() {}, destroyed() {} }) </script> |
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 | <script type= "module" > //1.导入 import Box from "./componnets/Box.js" import Nav1 from "./componnets/Nav1.js" import MyBox from "./componnets/MyBox.js" var vm = new Vue({ el: '#app' , data: {}, methods: {}, components: { //2.注册组件 Box, Nav1, MyBox, Box2:{ template:`<div> <h1>666-box2</h1> <p @click= "fn" >{{msg}}</p> </div>`, data: function (){ return {msg: "msg66666" } }, methods:{ fn(){console.log( "Box2组件的方法" )} }, filters: {}, watch: {}, computed: {}, directives: {}, beforeCreate() {}, created() {}, beforeMount() {}, mounted() {}, beforeUpdate() {}, updated() {}, beforeDestroy() {}, destroyed() {} } }, filters: {}, watch: {}, computed: {}, directives: {}, beforeCreate() {}, created() {}, beforeMount() {}, mounted() {}, beforeUpdate() {}, updated() {}, beforeDestroy() {}, destroyed() {} }) </script> |
本文来自博客园,作者:前端小白银,转载请注明原文链接:https://www.cnblogs.com/forever-ljf/p/16660137.html
分类:
Vue框架
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~