Javascript 当其中一个路由中的模板引用同一ID时,Vue路由不起作用

Javascript 当其中一个路由中的模板引用同一ID时,Vue路由不起作用,javascript,vue.js,vue-router,Javascript,Vue.js,Vue Router,我正在开发一个带有Vue路由的Vue应用程序。其中一个路由具有一个功能,即通过在各自的输入字段中写入所需的颜色来更改两个div的背景颜色。但有两个问题。路由器不工作,因为我写了这个颜色变化的功能,颜色变化也不工作。所以这基本上是一种“失败”的情况。我在Google inspection的控制台上收到了这样一条消息:“vue.js:597[vue warn]:找不到元素:#container” 我确信这些问题是相互交织的,问题是我如何解决它们 HTML <div id="container"

我正在开发一个带有Vue路由的Vue应用程序。其中一个路由具有一个功能,即通过在各自的输入字段中写入所需的颜色来更改两个div的背景颜色。但有两个问题。路由器不工作,因为我写了这个颜色变化的功能,颜色变化也不工作。所以这基本上是一种“失败”的情况。我在Google inspection的控制台上收到了这样一条消息:“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>
colorpage.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
});
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
const ColorPage={
模板:`
{{info}}


` }; var color=new Vue({ el:'集装箱', 数据:{ 信息:“通过键入颜色名称来更改颜色:”, bga:{ 背景颜色:“” }, bgb:{ 背景颜色:“” } } }); 导出默认颜色页
在colorpage.js中,您有了新的Vue()。这将创建一个新的Vue实例。您可能正在寻找的是创建一个新的


对于组件,您不需要定义在何处使用el安装它,而是由Vue路由器负责安装。在Vue应用程序中,您应该只定义一次el,而这正是Vue实例装载自身的地方。在Vue实例自行装载后,路由在Vue内部进行。

您是否有意使用两个Vue实例?老实说,我仍在学习Vue。如果我可以得到想要的结果,而只有一个实例,我会这样做!我在这里学习=)