提示:无法安装组件:模板或渲染功能未定义

时间:2019-01-10 19:34:25

标签: vue.js webpack

我想创建一个vue插件

我通过webpack构建了test.vue,打开index.html,出现此错误:无法装入组件:模板或渲染函数未定义。

Error screenshot

使用vue-cli构建的项目,请在main.js中使用

import VTest from './components/V-Test.vue'

Vue.use(VTest)

它将正常工作

但是使用链接方法不能直接在index.html中工作。

index.html

<!DOCTYPE html>
<html lang="zh" dir="ltr">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <title>V-TEST</title>
    <script src="../node_modules/vue/dist/vue.js" charset="utf-8"></script>
    <script src="../dist/main.js" charset="utf-8"></script>
</head>

<body>
    <div id="app">
        <v-test @click="handleLog"></v-test>
    </div>
</body>
<script type="text/javascript">
    new Vue({
        el: '#app',
        methods: {
            handleLog() {
                console.log('test.....')
            }
        }
    })
</script>

</html>

V-Test.vue

<template lang="html">
    <h1>this test word</h1>
</template>

<script>
export default {

}
</script>
<style lang="scss" scoped>

</style>

index.js

import VTest from './components/V-Test.vue'

const vTestPlugin = {
    install(Vue){
        Vue.component('v-test', VTest)
    }
}
if (typeof window !== 'undefined' && window.Vue) {
    window.Vue.use(vTestPlugin)
}

export default vTestPlugin

webpack.config.js

const path = require('path')
const {VueLoaderPlugin} = require('vue-loader')
const CleanWebpackPlugin = require("clean-webpack-plugin")

module.exports = {
    entry: './src/index.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js'
    },
    mode: 'development',
    devtool:'source-map',

    module: {
        rules: [{
                test: /\.vue$/,
                use: ['vue-loader', 'vue-style-loader', 'css-loader', 'sass-loader'],
                exclude: /node_modules/
            },
            {
                test: /\.js$/,
                use: 'babel-loader',
                include: path.resolve(__dirname, 'src'),
                exclude: /node_modules/
            }
        ]
    },
    plugins: [
        new VueLoaderPlugin(),
        new CleanWebpackPlugin(['dist'])
    ],
}

package.json

{
  "name": "v-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "molvqingtai",
  "license": "ISC",
  "dependencies": {},
  "devDependencies": {
    "@babel/core": "^7.2.2",
    "babel-loader": "^8.0.5",
    "babel-preset-env": "^1.7.0",
    "clean-webpack-plugin": "^1.0.0",
    "css-loader": "^2.1.0",
    "eslint": "^5.12.0",
    "node-sass": "^4.11.0",
    "sass-loader": "^7.1.0",
    "vue": "^2.5.21",
    "vue-loader": "^15.5.1",
    "vue-style-loader": "^4.1.2",
    "vue-template-compiler": "^2.5.21",
    "webpack": "^4.28.1",
    "webpack-cli": "^3.2.1"
  }
}

0 个答案:

没有答案