如果是Laravel Spark,则有一个具有以下内联模板的vue组件
<spark-update-payment-method-stripe :user="user" :team="team" :billable-type="billableType" inline-template>
/* ... */
<div class="pull-right">
<span v-if="billable.card_last_four">
<i :class="['fa', 'fa-btn', cardIcon]"></i>
************@{{ billable.card_last_four }}
</span>
</div>
/* ... */
</spark-update-payment-method-stripe>此模板包含变量billable.card_last_four。
如果我查找该组件的定义文件,我会看到以下内容
#File: resources/assets/js/spark-components/settings/payment-method/update-payment-method-stripe.js
var base = require('settings/payment-method/update-payment-method-stripe');
Vue.component('spark-update-payment-method-stripe', {
mixins: [base]
});如果我追踪到基本组件,我会看到一个定义的vue组件
#File: spark/resources/assets/js/settings/payment-method/update-payment-method-stripe.js
module.exports = {
props: ['user', 'team', 'billableType'],
/* ... */但是,这些组件似乎都没有定义billable anywhere。我看到很多关于this.billable的引用。
#File: spark/resources/assets/js/settings/payment-method/update-payment-method-stripe.js
/* ... */
this.form.address = this.billable.billing_address;
this.form.address_line_2 = this.billable.billing_address_line_2;
this.form.city = this.billable.billing_city;
this.form.state = this.billable.billing_state;
this.form.zip = this.billable.billing_zip;
this.form.country = this.billable.billing_country || 'US';
/* ... */
placeholder() {
if (this.billable.card_last_four) {
return `************${this.billable.card_last_four}`;
}
return '';
}
/* ... */这个billable属性是从哪里来的?我假设Vue正在做某种形式的元编程和/或魔法来填充它,但我对Vue还不够熟悉,无法知道发生了什么。
发布于 2017-07-29 02:43:53
在上面的Bert Evans和thanksd以及Chrome VueJS debugger的帮助下,我得到了我想要的答案
billable属性实际上是一个计算属性。但是,它不是在update-payment-method-stripe.js定义文件中本地计算的。相反,Spark有一个包含以下内容的vue-bootstrap.js
Vue.mixin(require('./mixin'));事实证明VueJS有一个global mixin特性(看起来像?)为系统中的每个组件添加一个方法。mixin模块如下所示
#File: spark/resources/assets/js/mixin.js
module.exports = {
computed: {
/**
* Get the billable entity.
*/
billable() {
/* ... */
},
/* ... */
}
};这意味着spark中的每个组件都将具有此计算机billable属性。
https://stackoverflow.com/questions/45379897
复制相似问题