组件路径:/views/home/components/bottom.vue 示例:
<template>
<div>
<h1>底部信息</h1>
<div>{{ copyright }} <button v-on:click="setTime">更新时间</button></div>
</div>
</template>
<script>
export default {
name: "bottom",
props: ["copyright"],//props:{copyright:String}
data() {
return {};
},
methods: {
setTime() {
this.$emit('TimeNow',{'name':'china'});
},
},
};
</script>
组件建立在components文件夹下,传值使用props,参数是数组形式,this.$emit是触发父组件的TimeNow事件,以此来调用父组件函数getTimeNow。
使用组件示例:
<template>
<div>
<h1>首页</h1>
<div>时间:{{tz}}-{{ dt }}</div>
<bottom copyright="xxxxx 2020" v-on:TimeNow="getTimeNow"></bottom>
</div>
</template>
<script>
import bottom from "@/views/home/components/bottom";
export default {
name: "home",
components: { bottom },
data() {
return {
dt: null,tz:''
};
},
methods: {
getTimeNow(timeZone) {
this.dt = new Date();this.tz=timeZone.name;
},
},
};
</script>
浙公网安备 33010602011771号