昨天按照教程安装好vue-router模块后,在应用中引入router 发现没有router.map方法,百度后原来是vue2.0里的路由改了
-
-
- const Foo = { template: '<div>foo</div>' }
- const Bar = { template: '<div>bar</div>' }
-
-
-
-
-
-
- const routes = [
- { path: '/foo', component: Foo },
- { path: '/bar', component: Bar }
- ]
-
-
-
- const router = new VueRouter({
- routes
- })
-
-
-
-
- const app = new Vue({
- router
- }).$mount('#app')
这是官方代码,这里有一个问题官方的组件写在index.html里的,我的是写在app.vue中的 所以第四步要写成
- / 4. 创建和挂载根实例。
-
-
- const app = new Vue({
- router:router,
- render: h => h(App)
- }).$mount('#app')
render
函数是渲染一个视图,然后提供给
el
挂载,所以不加的话就是空白什么都没有
上面的修改完后继续跑,接着又报错You are using the runtime-only build of Vue where the template option is not available.
弄了一下午愣是没弄好,最后在知乎上看到说是vue会打包生成3个文件一个是 runtime only 的文件 vue.common.js,一个是 compiler only 的文件 compiler.js,一个是 runtime + compiler 的文件 vue.js。
用 webpack 的别名功能把 vue/dist/vue.js 命名成了 vue,不然vue 的 package.json 中的 main 指向的是 dist/vue.common.js。
webpack.config.js
- <pre style="overflow: auto; font-family: monospace, monospace; font-size: 15px; margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); line-height: 22.5px;"><code class="language-text" style="font-family: Menlo, Monaco, Consolas, 'Andale Mono', 'lucida console', 'Courier New', monospace; font-size: 1em; overflow: auto;">resolve: {
- alias: {
- 'vue': 'vue/dist/vue.js'
- }
- },</code></pre><p></p>
- <pre></pre>
- 期待已久的页面总算出来了<br>
- <br>
- <br>
- <p></p>