Vue JS 2.0 不渲染任何东西?
Vue JS 2.0 not rendering anything?
使用Vue (^2.0.0-rc.6) + Browserify,入口点是index.js:
import Vue from 'vue'
import App from './containers/App.vue'
new Vue({ // eslint-disable-line no-new
el: '#root',
render: (h) => h(App)
})
App.vue:
<template>
<div id="root">
<hello></hello>
</div>
</template>
<script>
import Hello from '../components/Hello.vue'
export default {
components: {
Hello
}
}
</script>
<style>
body {
font-family: Helvetica, sans-serif;
}
</style>
Hello.vue:
<template>
<div class="hello">
<h1>\{{ msg }}</h1>
</div>
</template>
<script>
export default {
data () {
return {
msg: 'Hello Vue!'
}
}
}
</script>
白屏,我是不是错过了什么?
编辑:
条目 html 只是 <div id="root"></div>
,控制台日志上没有错误,我很确定 Hello.vue 已加载,因为 console.log('test')
该文件出现在控制台上.
编辑 2:
发现错误:
[Vue warn]: You are using the runtime-only build of Vue where the
template option is not available. Either pre-compile the templates
into render functions, or use the compiler-included build. (found in
anonymous component - use the "name" option for better debugging
messages.)
这是否意味着我必须使用 webpack 解决方案?不能使用标准 HTML?
解决方案:
从 'vue/dist/vue.js'
导入 Vue
只是为了让寻找答案的人们的生活更轻松:
import Vue from 'vue/dist/vue.js'
import App from './App.vue'
new Vue({
el: '#app',
render: h => h(App)
})
来自作者 -- 2.0 独立构建方式(编译器 + 运行时)。 NPM 包的默认导出仅为运行时,因为如果从 NPM 安装,您可能会使用构建工具预编译模板。
如果您使用的是像 browserify 或 Webpack 这样的构建工具,您很可能会使用 single file components 来避免此类错误(在单文件组件中,模板会自动编译为 vueify 渲染函数)。您绝对应该尽量避免在其他任何地方使用模板。查看论坛和文档以获取有关如何避免它们的答案。
但我根据自己的经验知道,在您的项目中找到导致错误消息的模板并不总是那么容易。如果您遇到同样的问题,作为临时解决方法,以下内容应该有所帮助:
你不应该导入 'vue/dist/vue.js'(查看文档:https://github.com/vuejs/vue/wiki/Vue-2.0-RC-Starter-Resources#standalone-vs-runtime-builds 为什么不)
相反,您应该在您使用的构建工具中处理它。
就我而言,我使用的是 browserify,您可以在其中使用 aliasify 来创建别名。将以下内容添加到您的 package.json
文件中:
{
// ...
"browser": {
"vue": "vue/dist/vue.common.js"
}
}
对于 Webpack 用户,您似乎必须将以下内容添加到您的配置中:
resolve: {
alias: {vue: 'vue/dist/vue.js'}
},
可以在文档中找到更多信息:https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only
使用 Brunch 我通过在 brunch-config.js
中添加此规则解决了这个问题:
npm: {
aliases: {
vue: "vue/dist/vue.js"
}
}
见http://brunch.io/docs/config#npm
这是构建一个带有内部<template>
的Vue组件:
<template>
<div> hello </div>
</template>
<script>
export default {
name: 'Hello',
props: {
title: String,
},
}
</script>
对于Vue 3.4.0
您可以在名为
的项目的根目录下添加一个新文件
vue.config.js
并在其中添加以下内容。
module.exports = {
runtimeCompiler: true
}
下次启动应用程序时可以看到
Compiled successfully in 204ms
20:46:46
App running at:
使用Vue (^2.0.0-rc.6) + Browserify,入口点是index.js:
import Vue from 'vue'
import App from './containers/App.vue'
new Vue({ // eslint-disable-line no-new
el: '#root',
render: (h) => h(App)
})
App.vue:
<template>
<div id="root">
<hello></hello>
</div>
</template>
<script>
import Hello from '../components/Hello.vue'
export default {
components: {
Hello
}
}
</script>
<style>
body {
font-family: Helvetica, sans-serif;
}
</style>
Hello.vue:
<template>
<div class="hello">
<h1>\{{ msg }}</h1>
</div>
</template>
<script>
export default {
data () {
return {
msg: 'Hello Vue!'
}
}
}
</script>
白屏,我是不是错过了什么?
编辑:
条目 html 只是 <div id="root"></div>
,控制台日志上没有错误,我很确定 Hello.vue 已加载,因为 console.log('test')
该文件出现在控制台上.
编辑 2:
发现错误:
[Vue warn]: You are using the runtime-only build of Vue where the template option is not available. Either pre-compile the templates into render functions, or use the compiler-included build. (found in anonymous component - use the "name" option for better debugging messages.)
这是否意味着我必须使用 webpack 解决方案?不能使用标准 HTML?
解决方案: 从 'vue/dist/vue.js'
导入 Vue只是为了让寻找答案的人们的生活更轻松:
import Vue from 'vue/dist/vue.js'
import App from './App.vue'
new Vue({
el: '#app',
render: h => h(App)
})
来自作者 -- 2.0 独立构建方式(编译器 + 运行时)。 NPM 包的默认导出仅为运行时,因为如果从 NPM 安装,您可能会使用构建工具预编译模板。
如果您使用的是像 browserify 或 Webpack 这样的构建工具,您很可能会使用 single file components 来避免此类错误(在单文件组件中,模板会自动编译为 vueify 渲染函数)。您绝对应该尽量避免在其他任何地方使用模板。查看论坛和文档以获取有关如何避免它们的答案。
但我根据自己的经验知道,在您的项目中找到导致错误消息的模板并不总是那么容易。如果您遇到同样的问题,作为临时解决方法,以下内容应该有所帮助:
你不应该导入 'vue/dist/vue.js'(查看文档:https://github.com/vuejs/vue/wiki/Vue-2.0-RC-Starter-Resources#standalone-vs-runtime-builds 为什么不)
相反,您应该在您使用的构建工具中处理它。
就我而言,我使用的是 browserify,您可以在其中使用 aliasify 来创建别名。将以下内容添加到您的 package.json
文件中:
{
// ...
"browser": {
"vue": "vue/dist/vue.common.js"
}
}
对于 Webpack 用户,您似乎必须将以下内容添加到您的配置中:
resolve: {
alias: {vue: 'vue/dist/vue.js'}
},
可以在文档中找到更多信息:https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only
使用 Brunch 我通过在 brunch-config.js
中添加此规则解决了这个问题:
npm: {
aliases: {
vue: "vue/dist/vue.js"
}
}
见http://brunch.io/docs/config#npm
这是构建一个带有内部<template>
的Vue组件:
<template>
<div> hello </div>
</template>
<script>
export default {
name: 'Hello',
props: {
title: String,
},
}
</script>
对于Vue 3.4.0
您可以在名为
vue.config.js
并在其中添加以下内容。
module.exports = {
runtimeCompiler: true
}
下次启动应用程序时可以看到
Compiled successfully in 204ms
20:46:46App running at: