2017-06-01 6 views
2

私のVueインスタンスのdataプロパティには、文字列の配列があります。Vue.js:一度に1要素ずつ配列の内容を出力します。

data() { 
    return { myArray: [] } 
} 

この配列には、文の単語が入ります。たとえば:

['Hi', 'my', 'name', 'is', 'foo'] 

私はテレプロンプターがどのように動作するかに類似の間で1秒の遅延、と一度に私のテンプレート一言で段落タグに出力文を、したいと思います:

[0秒] - ハイ

[1秒] - こんにちは私

[2秒] - こんにちは私の名前

[3秒] - こんにちは私の名前は

であります

[4秒] - こんにちは私の名前はfooです

何か助けていただければ幸いです。

+0

あなたはまず自分でいくつかのコードを書いてみると、その後問題が発生した場合に助けを求めるべきです - [ツアー]を取り、読んでください[尋ねます]。 – Picard

+1

@picardええ、それで楽しいことは何か:) 'vue.js'タグでは遅いです。 (JK :) – Bert

+0

@Picard haha​​、私の試みは新しい配列を作成し、それをテンプレートに文字列補間することですが、最初の配列の内容を新しい配列に移動しながら遅延させます。それが投稿に値するとは思わなかった。リンクありがとうございます、私は見てみましょう。 –

答えて

2

あなたはsetIntervalを使用し、各反復で単語を追加することができます。

new Vue({ 
 
    el: '#app', 
 
    data: { 
 
    array: ['Hi', 'my', 'name', 'is', 'foo'], 
 
    string: '' // here your string will be stored 
 
    }, 
 
    methods: { 
 
    appendWords: function() { 
 
     const it = this.array[Symbol.iterator](); // convenient for yeilding values 
 
     const int = setInterval(() => { // time interval 
 
     const next = it.next(); // next value 
 
     if (!next.done) { // done = true when the end of array reached 
 
      this.string += ' ' + next.value; // concatenate word to the string 
 
     } else { 
 
      clearInterval(int); // when done - clear interval 
 
     } 
 
     }, 1000) // interval duration, 1s 
 
    } 
 
    }, 
 
    mounted() { 
 
    this.appendWords(); // invoke function when ready 
 
    } 
 
});
<script src="https://unpkg.com/vue/dist/vue.js"></script> 
 

 
<div id="app"> 
 
    <p>{{ string }}</p> 
 
</div>

+0

まさに私が探していたものです。 javascriptのイテレータについて教えてくれてありがとう! –

+0

私はこれがどのように彼が求めていたものではないが大好きですが、LOLを受け入れました。そして文字通りのプログラマー(私)。 – Bert

+0

@BertEvans私はちょうどあなたの答えを見た!私の悪い、私は私の記述では不明であった。私は、この解決策がもたらすものであるテレプロンプター感覚を欲しかった。 –

1

コード

new Vue({ 
    el:"#app", 
    data:{ 
    base: ['Hi', 'my', 'name', 'is', 'foo'], 
    timed: [] 
    }, 
    mounted(){ 
    this.base.forEach((item, i) => { 
     setTimeout(() => { 
     this.timed.push(`[${i} sec] - ${this.base.slice(0, i + 1).join(" ")}`) 
     }, i * 1000) 
    }) 
    } 
}) 

テンプレート

<div id="app"> 
    <p v-for="time in timed">{{time}}</p> 
</div> 

Example

編集

ので、楽しみのために、私はあなたがJavaScriptでプロンプターのシミュレーションを行うことについては行くかもしれない方法を考えました。これはかなり素朴ですが、私がそれに入れた時間の間働きます。

new Vue({ 
 
    el:"#app", 
 
    data:{ 
 
    lines: [ 
 
     'Hi, my name is Foo. I am a literal', 
 
     'programmer. This is a teleprompter', 
 
     'simulation. These lines should all', 
 
     'be about the same length. There are ', 
 
     'different strategies for making that', 
 
     'happen.' 
 
    ], 
 
    show:[] 
 
    }, 
 
    mounted(){ 
 
    let currentIndex = 0 
 
    
 
    this.$el.addEventListener('animationend',() => { 
 
     this.show.push(this.lines[++currentIndex]) 
 
    }) 
 
    this.show.push(this.lines[0]) 
 
    } 
 
})
@keyframes revealText { 
 
    0% { 
 
    width:0%; 
 

 
    } 
 
    100% { 
 
    width:100%; 
 

 
    } 
 
} 
 

 
#app{ 
 
    width: 500px; 
 
    margin: auto 
 
} 
 

 
h1 { 
 
    white-space:nowrap; 
 
    overflow: hidden; 
 
    animation: revealText 5s; 
 
}
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script> 
 

 
<div id="app"> 
 
    <h1 v-for="line in show">{{line}}</h1> 
 
</div>

+0

@kevinshi私は興味があり、これで回った。あなたが気にしているかどうか見てください:) – Bert

+0

これはいいですね。 – wostex

+0

@wostexありがとう! CSSは本当に私の特権ではありません。私はトランジショングループや何かでこれを行うためのまともな方法があると思います。 – Bert

関連する問題