1

使用 vuejs 1 的代码是:

const App = Vue.extend(Index);
router.start(App, '#app');

组件在哪里Index

我尝试将其转换为 vue 2,如下所示:

const App = Vue.extend(Index);
const app = new App(router).$mount('#app');

但这给出了一个[Vue warn]: Error when rendering root instance.

重写这个的正确方法是什么?

谢谢

4

2 回答 2

4

您必须将路由器作为参数:

const app = new App({ router }).$mount('#app');

例子:

const Index = {
  template: `<div id="app">It's working!</div>`
}
const App = Vue.extend(Index)
const router = new VueRouter()

const app = new App({ router }).$mount('#app');
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<div id="app"></div>

于 2016-10-04T14:15:13.193 回答
2

试试这个(应用 == 索引)

const App = Vue.extend(require('./App.vue'))

const router = new VueRouter({
  routes,
  mode: 'abstract'
})

new App({
  router,
}).$mount('#app')
于 2016-10-04T14:15:09.867 回答