在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ Vue2.0 .vue文件中的組件無法生成

Vue2.0 .vue文件中的組件無法生成

Vue 2.0 通過Webpack導(dǎo)入vue文件組件。 webpack編譯通過。在瀏覽器運(yùn)行出如下問題:
clipboard.png
看了“webpack+vue的匹配報(bào)錯(cuò)”這個(gè)問題,https://segmentfault.com/q/10...。之后。

如果是建立const User={template:“<div> </div>”}可以運(yùn)行。
有什么辦法解決這個(gè)問題,求VueJS高手賜教,謝謝!

package.json:

{
  "name": "vue-route-learn",
  "version": "1.0.0",
  "description": "learn vue-router",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack"
  },
  "author": "Eason peng",
  "license": "MIT",
  "devDependencies": {
    "babel": "^6.23.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "css-loader": "^0.28.8",
    "style-loader": "^0.19.1",
    "vue": "^2.5.13",
    "vue-html-loader": "^1.2.4",
    "vue-loader": "^13.6.2",
    "vue-router": "^3.0.1",
    "vue-template-compiler": "^2.5.13",
    "webpack": "^3.10.0"
  }
}

webpack.config.js:

module.exports = {
    entry: __dirname + "/app/main.js",//已多次提及的唯一入口文件
    output: {
        path: __dirname + "/public",//打包后的文件存放的地方
        filename: "bundle.js"http://打包后輸出文件的文件名
    },
    devServer: {
        contentBase: "./public",//本地服務(wù)器所加載的頁面所在的目錄
        historyApiFallback: true,//不跳轉(zhuǎn)
        inline: true//實(shí)時(shí)刷新
    },
    module: {
        rules: [
            {
                test: /(\.jsx|\.js)$/,
                use: {
                    loader: "babel-loader",
                },
                exclude: /node_modules/
            },
            //解析VUE文件,vue最主要的就是用到了這個(gè)vue-loader,這才是解析vue文件的關(guān)鍵 
            //但是因?yàn)関ue文件里面又包含有css和js,所以才用到了后面的幾個(gè)loader 
            { test: /\.vue$/, loader: "vue-loader" },
            //解析css
            { test: /\.css$/, loader: "style!css" },
            //解析html
            { test: /\.(html|tpl)$/, loader: 'html-loader' }
        ]
    },
    resolve: {
        alias: {
            'vue$': 'vue/dist/vue.js'
        }
    }
}

入口main.js文件:

//main.js
import Vue from 'vue';
import VueRouter from 'vue-router';
// import User1 from '../views/user_component.js';
// import UserProfile from '../views/user_profile_component.js';
// import UserPosts from '../views/user_posts_component.js'
import User from '../views/user.vue';
import UserProfile from '../views/userprofile.vue';
import UserPosts from '../views/userposts.vue';

Vue.use(VueRouter)


//嵌套路由
// 1. 定義(路由)組件。
// 已經(jīng)從其他文件 import 進(jìn)來


// 2. 定義路由
// 每個(gè)路由應(yīng)該映射一個(gè)組件。 其中"component" 可以是
// 通過 Vue.extend() 創(chuàng)建的組件構(gòu)造器,
// 或者,只是一個(gè)組件配置對(duì)象。
// 我們晚點(diǎn)再討論嵌套路由。
const routes = [
    {
        path: '/user/:id', component: { User },
        children: [
            {
                // 當(dāng)/user/:id/profile 匹配成功
                // UserProfile 會(huì)被渲染在User 的<router-view>中
                path: 'profile',
                component: { UserProfile }
            },
            {
                // 當(dāng) /user/:id/posts 匹配成功
                // UserPosts 會(huì)被渲染在 User 的 <router-view> 中
                path: 'posts',
                component: { UserPosts }
            }
        ]
    },
]

// 3. 創(chuàng)建 router 實(shí)例,然后傳 `routes` 配置
// 你還可以傳別的配置參數(shù), 不過先這么簡單著吧。
const router = new VueRouter({
    routes // (縮寫)相當(dāng)于 routes: routes
})

// 4. 創(chuàng)建和掛載根實(shí)例。
// 記得要通過 router 配置參數(shù)注入路由,
// 從而讓整個(gè)應(yīng)用都有路由功能
const app = new Vue({
    router
}).$mount('#app')

// 現(xiàn)在,應(yīng)用已經(jīng)啟動(dòng)了!

user.vue:

<template>
    <div class="user">
        <h2> User {{ $route.params.id }} </h2>
        <router-view> </router-view>
    </div>
</template>

<script>
    export default {};
</script>

userposts.vue:

<template>
    <div class="posts">
        <div class="post">
            <h2> 1 </h2>
            </div>
        <div class="post">
            <h2> 2 </h2>
        </div>
        <div class="post">
            <h2> 3 </h2>
        </div>
    </div>
</template>

<script>
    export default {};
</script>

userprofile.vue

<template>
    <div class="profile">
        <h2> 1 </h2>
    </div>
</template>

<script>
    export default {};
</script>
回答
編輯回答
病癮

把 component 后面的括號(hào)去掉

const routes = [
    {
        path: '/user/:id', component: { User },
        children: [
            {
                // 當(dāng)/user/:id/profile 匹配成功
                // UserProfile 會(huì)被渲染在User 的<router-view>中
                path: 'profile',
               // component: { UserProfile }
                component: UserProfile 
            },
            {
                // 當(dāng) /user/:id/posts 匹配成功
                // UserPosts 會(huì)被渲染在 User 的 <router-view> 中
                path: 'posts',
                // component: { UserPosts }
                component: UserPosts 
            }
        ]
    },
]
2017年11月13日 04:21
編輯回答
心夠野

直接用vue-cli生成的項(xiàng)目吧,對(duì)比看看webpack的配置有啥區(qū)別

2017年9月16日 16:03