vue.jsで動的にcomponentの位置を変更できるコードを作りたいと思ってます。
例えば、
/A
ページにアクセスすると
[banner,article,article2,nav]
/Bページにアクセスすると
[article,article2,banner]
のようなdynamicな順序でcomponentを表示したいです。
尚、ページによって使わないcomponentが発生するので
必要なときだけ使うようにdynamic importを考えてます。
ここまでの考えで実装したのが以下のコードです。
<template> <component :is="whichStep"></component> </template> <script> export default { components: { 'Article': () => import('../components/Article'), 'Article2': () => import('../components/Article2'), 'Banner': () => import('../components/Banner'), 'Nav': () => import('../components/Nav') }, computed: { whichStep () { switch (key_number) { case 0: return 'Article' case 1: return 'Article2' case 2: return 'Banner' case 3: return 'Nav' default: return 'Error' } } </script>
これからは
const key_number_list = [2,0,1,3] //[banner,article,article2,nav] const key_number_list = [0,1,2] //[article,article2,banner]
のような変数を作って(こちらはページによってjsonとかで受け取る予定)
<component :is="whichStep"></component>
この部分をkey_number_list配列のlengthほど生成して各値に該当するcomponentをreturnするような
方法を考えてますが、どう実装すればわからずこちらに質問します。
そもそもvue.jsでcomponentの位置を動的に変更させるにこの実装方法は正しいのかも伺いしたいです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/08 07:57 編集