javascript - 当其中一个路由中的模板引用相同 ID 时,Vue-Routes 无法运行

标签 javascript vue.js vue-router

我正在使用 Vue-routes 开发 Vue 应用程序。其中一条路线有一个功能,即通过在各自的输入字段中写入所需的颜色来更改两个 div 的背景颜色。但有两个问题。自从我编写了这个颜色更改功能以来,路由器就无法工作,而且颜色更改也不起作用。所以这基本上是一种“失败-失败”的情况。我在 Google 检查中从控制台收到此消息:“vue.js:597 [Vue warn]:找不到元素:#container”。

我确信这些问题是相互交织的,问题是我该如何解决它们?

HTML

<div id="container" v-bind:style="bga">
   <div class="top" v-bind:style="bgb">
      <div id="app">
         <h1 id="vueHead">Vue routing</h1>
         <h2 class="menu">
            <router-link to="/">Color</router-link>
            <router-link to="/main">Main</router-link>
         </h2>
      </div>
   </div>
   <!-- component matched by the route will render here -->
   <router-view></router-view>
</div>

index.js(路线)

import MainPage from '../components/mainPage.js'
import ColorPage from '../components/colorPage.js'

var urlend;

const routes = [
    {path: '/', component: ColorPage},
    {path: '/main', component: MainPage},
    ];
const router = new VueRouter({
    routes // short for `routes: routes`
});
var vm = new Vue({
    el: '#container',
    router
});

colorpage.js

const ColorPage = {
    template: `
<div id="middle">
    <label id="colorLabel"><h2> {{ info }} </h2></label>
    </br>
    <input type="text" class="colorInput" v-on:input="bga.backgroundColor = $event.target.value" placeholder="here...">
    </br>
    <input type="text" class="colorInput"  v-on:input="bgb.backgroundColor = $event.target.value" placeholder="... and here!">
</div>
`
};
var color = new Vue({
    el: '#container',
    data: {
        info: 'Change Colors By Typing Their Names:',
        bga: {
            backgroundColor: ''
        },
        bgb: {
            backgroundColor: ''
        }
    }
});

export default ColorPage

最佳答案

在 colorpage.js 中有 new Vue()。这将创建一个新的 Vue 实例。您可能正在寻找的是创建一个新的 component .

对于组件,您不需要使用 el 定义将其挂载的位置,而是由 Vue router 来处理。在你的 Vue 应用程序中,你应该只定义一次 el ,这就是 Vue 实例安装自身的地方。 Vue 实例自行安装后,路由将在 Vue 内部发生。

关于javascript - 当其中一个路由中的模板引用相同 ID 时,Vue-Routes 无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49815988/

相关文章:

javascript - 为什么登录页面不使用javaScript?

javascript - 无法使用为 vue cli 3 设置添加的 babel-plugin-transform-object-rest-spread 传播对象

html - 在 Vue.js 中单击时在图像上添加点的最佳方法?

javascript - 如何使用 vue.js 从输入中获取值?

vue.js - 在 Front 中安全存储用户角色的位置

html - 如何在 Vue 路由器中定义要在组件内部使用的变量?

javascript - 单击时隐藏按钮

javascript - 在严格模式下获取未知环境中全局对象的引用

vue.js - 在组件内部使用时未定义 Dropzone

vue.js - 如何在 url 中删除查询?