【问题标题】:Set properties/ data in VueJs Component在 VueJs 组件中设置属性/数据
【发布时间】:2016-12-06 21:05:16
【问题描述】:

我有一个 VueComponent,我需要为其传递一个值“A”。问题是值 A 可以通过 VueRouter 参数或通过模板插值传递。通过这两个选项设置属性数据的最佳方法是什么?

例如

<component :property="value"></component>

如果我导航到

/component/8/edit

mounted() {
   this.property = this._route.params.id
}

【问题讨论】:

    标签: vue-component vue-router vuejs2


    【解决方案1】:

    你必须弄清楚你想优先考虑哪个参数,如果你想优先考虑route参数,你可以这样做:

    mounted() {
       this.property = this.$route.params.id ? this.$route.params.id  : this.property
    }
    

    如果要优先通过props,可以这样写:

    mounted() {
       this.property = this.property ? this.property : this.$route.params.id 
    }
    

    【讨论】:

    • 稍微偏离主题但相关的“数据”属性可以在模板语法中引用时成为函数吗?即data{ return { property(){ return this.otherProperty } } } 还是应该留待计算,因为我想做你所说的并且能够根据需要覆盖
    • 在聊天中给你留言
    猜你喜欢
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 2020-11-09
    相关资源
    最近更新 更多