1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Vue.js 状態管理用の変数を実装してみた

Last updated at Posted at 2022-03-22

実現したかったこと

aタグなどの要素としては同じだが遷移先が異なるものについてcomponent化
生成した数に応じて、表示位置を制御するクラスを適用する

実装

index.html
<!-- // ... -->
   <body>
        <!-- // ... -->

        <vue_header></vue_header>

        <!-- // ... -->
    </body>
    
    <script type="text/javascript">
        Vue.component('vue_a', {
            props: [
                'list',
                'position_class',
            ],
            template: `
                <a
                    v-bind:class="position_class"
                    v-bind:href="list.href"
                >
                    {{ list.name }}
                </a>
            `,
        });
        
        Vue.component('vue_header', {
            beforeCreate: function () {
                this.count = 0;
            },
            data: function () {
                return {
                    lists: [
                        {
                            href: "/href1",
                            name: "リンク1",
                        },
                        {
                            href: "/href2",
                            name: "リンク2",
                        },
                        {
                            href: "/href3",
                            name: "リンク3",
                        },
                    ],
                };
            },
            methods: {
                getPositionClass: function () {
                    if (this.count < 2) {
                        this.count++;
    
                        return "left";
                    } else {
                        this.count++;
    
                        return "right";
                    }
                }
            },
            template: `
                <header>
                    <div>
                        <vue_a 
                            v-for="(list, index) in this.lists"
                            v-bind:key=index
                            v-bind:list=list
                            v-bind:position_class="getPositionClass()"
                        >
                        </vue_a>
                    </div>
                </header>
            `,
        });

        <!-- // ... -->
    </script>

<!-- // ... -->

つまずいたところ

vue_headerのdata内にcountを保持していたが、
countの更新と共にtemplateが再描画されてしまう

beforeCreate内で変数初期化することで、
処理内で更新してもtemplateは更新されない

さいごに

他にいい対応方法ありましたら、コメントで指摘して頂けるとありがたいです...

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?