【发布时间】:2016-10-26 22:07:36
【问题描述】:
我目前正在开发的网站是在 Drupal 7 中构建的。我有一个需要用户输入的表单,因此我正在尝试使用 VueJS 构建它。该表单全部包含在一个模板文件 (.tpl.php) 中,所有内容都在此模板文件中或通过 VueJS Javascript 提供(没有来自 CMS)。
我遇到的问题是 Vue 组件没有在前端呈现,但是当我将代码复制到 JSFiddle 时它们会呈现,所以我猜测这是 VueJS 和 Drupal 之间的交互问题。这是我检查时标记的屏幕截图...
这是来自 .tpl.php 文件的代码...
<div id="app">
<form>
<div>
<label for="year">Per Year</label>
<input type="radio" name="frequency" id="year" value="year" v-model="frequency" checked>
<label for="month">Per Month</label>
<input type="radio" name="frequency" id="month" value="month" v-model="frequency">
</div>
</form>
<ul class="plans">
<template id="plan-component">
<h2 class="plan-name">{{ name }}</h2>
<h2 class="plan-cost">{{ price }}</h2>
<h2 class="plan-tagline">{{ tagline }}</h2>
<a href="#" v-on:click="makeActivePlan($event)" class="select-plan button">Choose this plan</a>
</template>
<li>
<plan-component :frequency="frequency"
name="Basic"
tagline="Basic tagline"
price-yearly="Free"
price-monthly="Free"
></plan-component>
</li>
<li>
<plan-component :frequency="frequency"
name="Rec"
tagline="Rec tagline"
price-yearly="3"
price-monthly="4"
></plan-component>
</li>
<li>
<plan-component :frequency="frequency"
name="Team"
tagline="Team tagline"
price-yearly="4"
price-monthly="5"
></plan-component>
</li>
<li>
<plan-component :frequency="frequency"
name="Club"
tagline="Club tagline"
price-yearly="5"
price-monthly="6"
></plan-component>
</li>
</ul>
</div>
..以及我的 JS 文件中的代码...
Vue.component('plan-component', {
template: '#plan-component',
props: ['frequency', 'name', 'tagline', 'priceYearly', 'priceMonthly'],
computed: {
'price': function() {
if (this.frequency === 'year') {
return this.priceYearly;
} else {
return this.priceMonthly;
}
}
},
methods: {
makeActivePlan() {
// We dispatch an event setting this to become the active plan
this.$dispatch('set-active-plan', this);
}
}
});
new Vue({
el: '#app',
data: {
frequency: 'year',
activePlan: {name: 'no', price: 'You must select a plan!' }
},
events: {
'set-active-plan': function(plan) {
this.activePlan = plan;
}
},
});
这是正确输出组件的 JSFiddle - https://jsfiddle.net/2xgrpLm6/
【问题讨论】:
-
控制台有错误吗?