首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >vue组件无法加载

vue组件无法加载
EN

Stack Overflow用户
提问于 2018-01-27 12:40:11
回答 2查看 2K关注 0票数 0

我尝试在laravel中使用vuejs,我安装了npm vue-router vue-axios,但当我试图加载我的页面时,我得到了类似于:ReferenceError: CreateCategory is not defined和empty page的控制台错误。

这是我的app.js

代码语言:javascript
复制
require('./bootstrap');

window.Vue = require('vue');

import VueRouter from 'vue-router';
Vue.use(VueRouter);

import VueAxios from 'vue-axios';
import axios from 'axios';
Vue.use(VueAxios, axios);


import App from './App.vue';
Vue.component('CreateCategory', require('./components/CreateCategory.vue'));
Vue.component('DisplayCategory', require('./components/DisplayCategory.vue'));
Vue.component('EditCategory', require('./components/EditCategory.vue'));


const routes = [
  {
    name: 'CreateCategory',
    path: '/categories/create',
    component: CreateCategory
  },
  {
    name: 'DisplayCategory',
    path: '/',
    component: DisplayCategory
  },
  {
    name: 'EditCategory',
    path: '/edit/:id',
    component: EditCategory
   }
];



const router = new VueRouter({ mode: 'history', routes: routes});
new Vue(Vue.util.extend({ router }, App)).$mount('#app');

// const app = new Vue({
//   router,
//   render: h => h(App)
// });

// const app = new Vue({
//     el: '#app'
// });

PS:我读过关于路由器中组件的文章,我已经尝试过.default,但它们也不能工作,唯一的好处是我没有得到控制台错误。

更新

我的CreateCategory.vue组件:

代码语言:javascript
复制
<template>
  <div>
    <h1>Create A Category</h1>
    <form v-on:submit.prevent="addCategory">
      <div class="row">
        <div class="col-md-6">
          <div class="form-group">
            <label>Category Title:</label>
            <input type="text" class="form-control" v-model="category.title">
          </div>
        </div>
        </div>
        <div class="row">
          <div class="col-md-6">
            <div class="form-group">
              <label>Category Status:</label>
              <input type="text" class="form-control col-md-6" v-model="category.status" />
            </div>
          </div>
        </div><br />
        <div class="form-group">
          <button class="btn btn-primary">Add Category</button>
        </div>
    </form>
  </div>
</template>

<script>
  export default {
    data(){
        return{
          category:{}
        }
    },
    methods: {
      addCategory(){
        let uri = 'http://localhost/vuetjd/public/categories';
        this.axios.post(uri, this.category).then((response) => {
          this.$router.push({title: 'DisplayCategory'})
        })
    }
  }
}
</script>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-27 23:17:35

我发现了问题所在,我应该运行php artisan serve来查看我的数据。如果只是打开我的网址,我会得到blanck页面,但对于serve,我也会得到我的数据。

票数 0
EN

Stack Overflow用户

发布于 2018-01-27 16:35:35

CreateCategory.vue是单个文件组件。

在那里,您必须有一个组件,其中{}实际上是export default {}对象。

您必须做的是,您需要导入CreateCategory.vue,然后分配它,如下所示:

代码语言:javascript
复制
import CreateCategory from './components/CreateCategory.vue';

const routes = [
  {
    name: 'CreateCategory',
    path: '/categories/create',
    component: CreateCategory
  }
];

现在,这将会起作用。您必须对DisplayCategoryEditCategory执行相同的操作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48472658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档