2017-08-30 77 views
0

我想学习Vue.JS,并且我所做的组件不是在页面上呈现。这是我的组件:简单Vue组件不呈现

import Vue from 'vue' 

const card = new Vue({ 
    el: '#card', 
    data: { 
    title: 'Dinosaurs', 
    content: '<strong>Dinosaurs</strong> are a diverse group of animals 
from the clade <em>Dinosauria</em> that first appeared at the Triassic 
period.' 
    } 
}) 

这是我的html:

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="utf-8"> 
    <title>Vue Practice</title> 
    </head> 
    <body> 
    <div id="card"> 
     <header>{{ title }}</header> 
     <div v-html="content"></div> 
    </div> 
    <script src="bundle.js"></script> 
    </body> 
</html> 

这是的package.json(我知道大多数的依赖性属于在devDependencies):

{ 
     "name": "vue-practice", 
     "version": "1.0.0", 
     "description": "", 
     "main": "index.js", 
     "scripts": { 
     "bundle": "browserify -t babelify -t vueify -e main.js > 
        bundle.js", 
     "test": "echo \"Error: no test specified\" && exit 1" 
     }, 
     "keywords": [], 
     "author": "", 
     "license": "ISC", 
     "dependencies": { 
     "babel-plugin-transform-es2015-modules-commonjs": "^6.26.0", 
     "babelify": "^7.3.0", 
     "browserify": "^14.4.0", 
     "vue": "^2.4.2", 
     "vueify": "^9.4.1" 
     }, 
     "devDependencies": {} 
    } 

当我在浏览器中加载index.html,它呈现的只是{{title}},我没有收到任何错误。任何解释为什么发生这种情况将不胜感激!

+0

你使用Vue + Compiler包吗?发布你的'package.json'。 – yuriy636

+0

我认为vueify会完成这项工作,但我会发布整个package.json。 –

回答

2

vueify只转换*.vue文件,如果你要在你的index.html使用的模板,那么你需要的Vue编译器为了编译浏览器(https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only)的模板。

一个好办法有这样的事情覆盖是使用vuejs-template,如果您正在使用Browserify有一个:https://github.com/vuejs-templates/browserify

但是当你有几乎一切,你只能添加缺少了什么编译器如Vue指南中的“Runtime + Compiler vs. Runtime-only” - “Browserify”部分所述。

添加到您的package.json

{ 
    "browser": { 
     "vue": "vue/dist/vue.common.js" 
    }, 
} 

会告诉Browserify使用完整的(也称为独立)Vue公司建立在浏览器中。


另一种方式是在index.html而是在主要成分不使用模板,像App.vue,对于你看看我上面链接的模板,它是开箱即用。

+0

谢谢你,很好的解释。 –