2017年6月30日 星期五

(vue)另一種切換模版內容的方式(模版 data 不變)

<!DOCTYPE html>
<html lang="en">

<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="../js_lib/vue-2.3.0.js"></script>
    <script>
 Vue.component('child', {
    template: `<div>
            <h1 v-if="level === 1">
                1
            </h1>
            <h1 v-if="level === 2">
                2
            </h1>
            <h1 v-if="level === 3">
                3
            </h1>
        </div>`,
    props: {
        level: {
            type: Number
        }
    }
});


</script>
</head>

<body>
    <div id="app">
        <span v-for="i in 3">
            {{i}}:
            <input type="radio" v-model="level" :value="i">
        </span>
        <child ref="child" :level="level"></child>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                level: 1
            }
        });
    </script>
</body>

</html>