使用Laravel 5.3,我在/resources/assets/js/app.js
中有以下内容:
require('./bootstrap');
require('./components/test-component'); // custom
const app = new Vue({
el: '#app'
});
组件test-component.js
只是
Vue.component('test-component', {
data() {
return {
someData: '' // does work!
};
},
mounted() {
console.log('ready'); // does not fire...
},
methods: {
testMethod() {
console.log('here'); // does not fire...
}
}
});
然后在Blade模板中
<test-component>
<form>
<!-- a bunch of HTML markup here... -->
<button type="button" @click="testMethod">Fire a test method...</button>
</form>
</test-component>
单击按钮时,控制台中不显示任何内容。此外,加载表单时mounted
不会触发。同时,someData
模型绑定完美无缺!
我在这里做错了什么?
答案 0 :(得分:0)
您需要将模板添加到组件选项:
Vue.component('test-component', {
template: ` <form>
<!-- a bunch of HTML markup here... -->
<button type="button" @click="testMethod">Fire a test method...</button>
</form>`,
//..
});
答案 1 :(得分:0)
您应该添加模板:
Vue.component('test-component', {
data() {
return {
someData: '' // does work!
};
},
template: '<div></div>', // add your template here
mounted() {
console.log('ready'); // does not fire...
},
methods: {
testMethod() {
console.log('here'); // does not fire...
}
}
});