Vueify / VueRouter / Laravel - 组件未定义
Vueify / VueRouter / Laravel - Component not defined
我正在构建一个相对简单的 laravel 应用程序并想学习 Vue.js...这变成了一个非常令人沮丧的混乱...
不管怎样,问题来了。无论我做什么,我都会收到未定义我的组件的错误。
这是我的...
Gulpfile
var elixir = require('laravel-elixir');
require('laravel-elixir-vueify');
// Elixir Mixins.
elixir(function(mix) {
mix.sass('app.scss');
mix.browserify('main.js');
});
/resources/assets/js/main.js
var Vue = require('vue');
var VueRouter = require('vue-router');
Vue.use(VueRouter);
import Form2016_1099_misc from './components/2016-1099-misc.vue';
Vue.component('Form2016_1099_misc', Form2016_1099_misc);
在我的 blade 文件中...
@section('content')
<div id="app">
<h1>Hello App!</h1>
<p>
<!-- use v-link directive for navigation. -->
<a v-link="{ path: '/recipient' }">Go to Recipient</a>
<a v-link="{ path: '/form/2016/1099-misc' }">Go to 1099</a>
</p>
<!-- route outlet -->
<router-view></router-view>
</div>
@endsection
@section('footer')
<script src="{{ asset('/js/main.js') }}"></script>
<script>
// Define some components
var RecipientForm = Vue.extend({
template: '<p>This is the recipient form.</p>'
});
var App = Vue.extend({});
var router = new VueRouter();
router.map({
'/recipient': {
component: RecipientForm
},
'/form/2016/1099-misc': {
component: Form2016_1099_misc
},
});
router.start(App, '#app');
</script>
@endsection
/resources/assets/js/components/2016-1099-misc.vue
<template>
{{ msg }}
</template>
<style>
</style>
<script>
export default{
data(){
return{
msg: 'This is a test'
}
}
}
</script>
I 运行 gulp 运行成功。当我在浏览器中查看页面时,收到错误消息
ReferenceError: Form2016_1099_misc is not defined
我确定我做错了很多事情。我是 Vue 的超级新手,我正在努力掌握它...
更新:
我已将我的 blade 文件中的所有脚本代码移动到 main.js 文件中,现在 main.js 看起来像这样 -
var Vue = require('vue');
var VueRouter = require('vue-router');
use(VueRouter);
import Form2016_1099_misc from './components/2016-1099-misc.vue';
Vue.component('Form2016_1099_misc', Form2016_1099_misc);
// Define some components
var RecipientForm = Vue.extend({
template: '<p>This is the recipient form.</p>'
});
var App = Vue.extend({});
var router = new VueRouter();
router.map({
'/recipient': {
component: RecipientForm
},
'/form/2016/1099-misc': {
component: Form2016_1099_misc
},
});
router.start(App, '#app');
我现在收到错误消息
[Vue warn]: Failed to resolve directive: link (found in component: <router-app>)
[Vue warn]: Unknown custom element: <router-view> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
您需要使用 Vue 注册该组件。你告诉路由器在路由改变时构建一个组件视图,但是 Vue 也需要知道组件。
导入组件后,尝试:
Vue.component('Form2016_1099_misc', Form2016_1099_misc);
我显然加载了两次 Vue。一次来自 NPM,一次来自 CDN。我删除了 CDN,现在它工作正常...
我正在构建一个相对简单的 laravel 应用程序并想学习 Vue.js...这变成了一个非常令人沮丧的混乱...
不管怎样,问题来了。无论我做什么,我都会收到未定义我的组件的错误。
这是我的...
Gulpfile
var elixir = require('laravel-elixir');
require('laravel-elixir-vueify');
// Elixir Mixins.
elixir(function(mix) {
mix.sass('app.scss');
mix.browserify('main.js');
});
/resources/assets/js/main.js
var Vue = require('vue');
var VueRouter = require('vue-router');
Vue.use(VueRouter);
import Form2016_1099_misc from './components/2016-1099-misc.vue';
Vue.component('Form2016_1099_misc', Form2016_1099_misc);
在我的 blade 文件中...
@section('content')
<div id="app">
<h1>Hello App!</h1>
<p>
<!-- use v-link directive for navigation. -->
<a v-link="{ path: '/recipient' }">Go to Recipient</a>
<a v-link="{ path: '/form/2016/1099-misc' }">Go to 1099</a>
</p>
<!-- route outlet -->
<router-view></router-view>
</div>
@endsection
@section('footer')
<script src="{{ asset('/js/main.js') }}"></script>
<script>
// Define some components
var RecipientForm = Vue.extend({
template: '<p>This is the recipient form.</p>'
});
var App = Vue.extend({});
var router = new VueRouter();
router.map({
'/recipient': {
component: RecipientForm
},
'/form/2016/1099-misc': {
component: Form2016_1099_misc
},
});
router.start(App, '#app');
</script>
@endsection
/resources/assets/js/components/2016-1099-misc.vue
<template>
{{ msg }}
</template>
<style>
</style>
<script>
export default{
data(){
return{
msg: 'This is a test'
}
}
}
</script>
I 运行 gulp 运行成功。当我在浏览器中查看页面时,收到错误消息
ReferenceError: Form2016_1099_misc is not defined
我确定我做错了很多事情。我是 Vue 的超级新手,我正在努力掌握它...
更新:
我已将我的 blade 文件中的所有脚本代码移动到 main.js 文件中,现在 main.js 看起来像这样 -
var Vue = require('vue');
var VueRouter = require('vue-router');
use(VueRouter);
import Form2016_1099_misc from './components/2016-1099-misc.vue';
Vue.component('Form2016_1099_misc', Form2016_1099_misc);
// Define some components
var RecipientForm = Vue.extend({
template: '<p>This is the recipient form.</p>'
});
var App = Vue.extend({});
var router = new VueRouter();
router.map({
'/recipient': {
component: RecipientForm
},
'/form/2016/1099-misc': {
component: Form2016_1099_misc
},
});
router.start(App, '#app');
我现在收到错误消息
[Vue warn]: Failed to resolve directive: link (found in component: <router-app>)
[Vue warn]: Unknown custom element: <router-view> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
您需要使用 Vue 注册该组件。你告诉路由器在路由改变时构建一个组件视图,但是 Vue 也需要知道组件。
导入组件后,尝试:
Vue.component('Form2016_1099_misc', Form2016_1099_misc);
我显然加载了两次 Vue。一次来自 NPM,一次来自 CDN。我删除了 CDN,现在它工作正常...