做页面的时候有一些公用的代码作为自定义组件输出
自定义组件又称为自定义标签
项目中components就是组件的意思
在目录中新建目录components
然后新建组件helloComp.vue
<template><view></view></template><script>export default {name:"helloComp",data() {return {};}}</script><style></style>
要作为模板使用,因为要被其它页面所调用,所以要定义一个自定义标签
定义组件名称
<template name="helloComp">
export default {// 定义组件名称为helloCompname:"helloComp",data() {return {};}
在页面中做一个输出
data()是和当前页面的模板数据进行绑定的
data() {return {};}
整个组件呈现
<!-- 定义组件名称为helloComp --><template name="helloComp"><view></view></template><script>export default {// 定义组件名称为helloCompname:"helloComp",data() {return {msg: "这是自定义组件"};}}</script><style></style>
外部文件引用
script中引用
import helloComp from "../../components/helloComp.vue";
注册
在export default 中写入
components:{helloComp}
接下里就可以作为标签使用
<helloComp>xxxxxx</helloComp>
