【发布时间】:2019-06-09 20:44:54
【问题描述】:
我是 Vue.js 的新手,我对文件结构以及如何构建简单的应用程序感到困惑。
我已经使用这个命令在我的 Mac 上安装了 Vue CLI:
npm install -g @vue/cli
然后我创建了一个计数器项目并使用了默认选项:
vue create counter
然后我启动了应用程序:
cd counter
npm run serve
默认应用程序代码让我感到困惑,所以我想创建自己的简单应用程序,对我来说更有意义:
我在公共文件夹中创建了 counter.html:
<html lang="en">
<body>
<div id="counter"></div>
</body>
<script src="../src/counter.js" type="text/javascript"></script>
</html>
我在 src 文件夹中创建了 counter.js 文件:
import Vue from 'vue';
import Counter from './components/counter.vue';
new Vue({
render: h => h(Counter),
}).$mount('#counter')
我在 components 文件夹中创建了 counter.vue 文件:
<template>
<button v-on:click="count++">You clicked me {{ count }} times.</button>
</template>
<script type="text/javascript">
export default {
name: 'Counter',
props: [
'count'
],
}
</script>
然后我运行:
npm run build
当我访问我的页面时:http://localhost:8080/counter.html 我得到一个空白页,控制台显示以下错误:Uncaught SyntaxError: Unexpected token
非常感谢任何有关我做错的帮助。
【问题讨论】:
-
还有其他信息吗?代码中的位置行数?
npm run serve显示带有完整堆栈跟踪的详细统计信息。 -
counter.vue中的模板有一个浮动撇号 -
我删除了浮动撇号,但仍然是相同的空白页和控制台日志错误。 npm run serve 显示已编译成功应用程序运行的标准消息:localhost:8080
-
如果将 id 更改为“app”并将挂载更改为“#app”会发生什么。我很确定这是默认 Vue CLI 安装所必需的。
-
另外,您的脚本标签位于
counter.html中的无效位置。它需要进入head或body,但不能在结束body标签之后。
标签: javascript vue.js vuejs2 vue-component