vue.js - 如何在应用程序外部安装路由器链接?

标签 vue.js

我用的是guide/Getting started的基础教程, 但将 HTML 代码更改为

<div id="app">
  <!-- ... all the same except router-view ... -->
</div>

<h2>Outside:</h2>
<code id="outApp"><router-view></router-view></code>

但是,当然,它不起作用,我需要说在#outApp 挂载router-view,怎么做?

PS:当 #outApp 位于 #app 内时一切正常,在我的页面上复制教程。

最佳答案

如前所述,并确保您已获悉此不切实际的做法。这就是我为您提供的解决方案。

创建一个新的 Vue 实例 (#app2) 并在其中使用路由器。

Vue.config.productionTip = Vue.config.devtools = false // ignore
const router = new VueRouter({
  routes: [
{
  path: "/",
  component: { template: "<router-view/>" },
  children: [
    {
      path: "",
      name: "foo",
      component: { template: "<div>Foo</div>" }
    },
    {
      path: "bar",
      name: "bar",
      component: { template: "<div>Bar</div>" }
    }
  ]
}
  ]
});

// App1 without routing capabilities
new Vue({
  // router, don't need it here
  el: "#app1"
});

// App2
new Vue({
  router,
  el: "#app2"
});
<script src="https://unpkg.com/vue@2.5.16"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>

<fieldset>
  <legend>App1</legend>
  <div id="app1">
This is the app without router
  </div>
</fieldset>

<fieldset>
  <legend>App2</legend>
  <div id="app2">
<router-link :to="{name:'foo'}">Foo</router-link>
<router-link :to="{name: 'bar'}">Bar</router-link>
<hr>
<router-view></router-view>
  </div>
</fieldset>

关于vue.js - 如何在应用程序外部安装路由器链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50546348/

相关文章:

javascript - 如何从 vue 组件调用 App.vue 中的方法

javascript - 如何使用 vuejs 绑定(bind)来自服务器的动态字段

javascript - Array.push() 不起作用,推送后返回空数组

javascript - 如果模型被覆盖,Vue.js 指令 v-html 不会更新

vue.js - Webpack +SCSS错误: "Module parse failed: Unexpected character ' @' (1:0)"

vue.js - Vue-cli 3 : "command failed: npm install --loglevel error"

testing - VUE CLI 3 - 使用 Mocha + webpack 测试单文件组件

vue.js - 在nuxt中加载页面时如何显示组件

javascript - [Vue warn] : Invalid prop: type check failed for prop "xxx". 预期值为 X 的数字,得到值为 "X"的字符串

vue.js - 我可以在不使用 ul 或 ol 的情况下使用 Vue v-for