如何在Vue Routes应用程序中动态更改颜色?

时间:2018-04-14 09:42:49

标签: javascript vue.js vue-router

我正在尝试创建一个Vue-Routes应用程序,通过在两个输入字段中写入所选颜色的名称,可以在index.html中更改两个div的颜色。这有点复杂,因为这是一个路线应用程序。我如何完成这项任务?

完成的应用程序应具有此功能:Codepen

index.js(路线)

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

const routes = [
    {path: '/', component: ColorPage},
];
const router = new VueRouter({
    routes

});

var vm = new Vue({
    el: '.container',
    router,
});

ColorPage.js

const ColorPage = {
    props:
        ['bga', 'bgb'],
    data() {
        bga: {
            backgroundColor: ''
        },
        bgb: {
            backgroundColor: ''
        }
    },
    template: `
<div id="middle">
    <label id="colorLabel"><h2>'Change Colors By Typing Their Names:'</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>
`,
};

export default ColorPage

的index.html

<body>
    <div class="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>
                    <router-link to="/settings">Settings</router-link>
                    <router-link to="/vue">Vue</router-link>
                </h2>
            </div>
        </div>

        <router-view></router-view>

    </div>
</body>

1 个答案:

答案 0 :(得分:1)

您还需要在父级中定义bgabgb

&#13;
&#13;
const ColorPage = {
    props:['bga','bgb'],
    template: `
<div id="middle">
    <label id="colorLabel"><h2>Change Colors By Typing Their Names:</h2></label>
    </br>
    <input type="text" class="colorInput" v-model="$parent.bga" placeholder="here...">
    </br>
    <input type="text" class="colorInput" v-model="$parent.bgb" placeholder="... and here!">
</div>
`
};

const routes = [
    {path: '/', component: ColorPage}
];
const router = new VueRouter({
    routes

});

var vm = new Vue({
    el: '.container',
    router,
    data:{
        bga:'',
        bgb:''
    }
});
&#13;
<script type="text/javascript" src="https://unpkg.com/vue@2.5.16/dist/vue.js"></script>
<script type="text/javascript" src="https://unpkg.com/vue-router@2.7.0/dist/vue-router.js"></script>
<div class="container">
<div v-bind:style="bga">

        <div class="top" v-bind:style="'background-color:'+bga+';'">
            <div id="app">
                <h1 id="vueHead" v-bind:style="'color:'+bgb+';'">Vue routing</h1>
                <h2 class="menu">
                    <router-link to="/">Color</router-link>
                    <router-link to="/main">Main</router-link>
                    <router-link to="/settings">Settings</router-link>
                    <router-link to="/vue">Vue</router-link>
                </h2>
            </div>
        </div>

        <router-view></router-view>

    </div>
    
   </div>
&#13;
&#13;
&#13;

另一种方式是$parent

&#13;
&#13;
const ColorPage = {
    data:function(){
      return {
          bga: this.$parent.bga,
          bgb: this.$parent.bgb,
        }
    },
    methods:{
      changeBgaColor:function(){
        this.$parent.bga = this.bga;
      },
      changeBgbColor:function(){
        this.$parent.bgb = this.bgb;
      }
    },
    template: `
<div id="middle">
    <label id="colorLabel"><h2>Change Colors By Typing Their Names:</h2></label>
    </br>
    <input type="text" class="colorInput" v-on:keyup="changeBgaColor" v-model="bga" placeholder="here...">
    </br>
    <input type="text" class="colorInput" v-on:keyup="changeBgbColor" v-model="bgb" placeholder="... and here!">
</div>
`
};

const routes = [
    {path: '/', component: ColorPage}
];
const router = new VueRouter({
    routes

});

var vm = new Vue({
    el: '.container',
    router,
    data:{
        bga:'',
        bgb:''
    }
});
&#13;
<script type="text/javascript" src="https://unpkg.com/vue@2.5.16/dist/vue.js"></script>
<script type="text/javascript" src="https://unpkg.com/vue-router@2.7.0/dist/vue-router.js"></script>
<div class="container">
<div v-bind:style="bga">

        <div class="top" v-bind:style="'background-color:'+bga+';'">
            <div id="app">
                <h1 id="vueHead" v-bind:style="'color:'+bgb+';'">Vue routing</h1>
                <h2 class="menu">
                    <router-link to="/">Color</router-link>
                    <router-link to="/main">Main</router-link>
                    <router-link to="/settings">Settings</router-link>
                    <router-link to="/vue">Vue</router-link>
                </h2>
            </div>
        </div>

        <router-view></router-view>

    </div>
    
   </div>
&#13;
&#13;
&#13;