vue2 : Cannot find element: #app

第一种

App.vue

<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>

index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1.0, user-scalable=no">
  <title>vue</title>
</head>
<body>

</body>
</html>

app.js

const app = new Vue({
  router,
  nprogress,
  ...App 
})

app.$mount('#app')

第二种

index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1.0, user-scalable=no">
  <title>vue</title>
</head>
<body>
<div id="app">
<router-view></router-view>
</div>
</body>
</html>

app.js

const app = new Vue({
  router,
  nprogress,
})

app.$mount('#app')

为什么第一种显示Cannot find element: #app ,而第二种可以?https://github.com/vuejs/vue-... 也是第一种写法,不知道哪里出了问题?

阅读 9.3k
2 个回答

第一种,因为挂载的时候找不到id为app的元素

那个项目可以貌似因为是分客户端渲染和服务端渲染的

你确定你引入 App.vue 了,且路径正确?

import App from './App.vue'
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题