vue.js组件使用components和参数传递


组件路径:/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"],
  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>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM