官术网_书友最值得收藏!

Mustache template example

Let's begin working with our first pen:

<div id="entryPoint">
<h1>Just an h1 heading here</h1>
<h2>Just an h2 heading here</h2>
<p>Vue JS is fun</p>
</div>

We can now see our HTML being rendered in the CodePen preview pane, with the following text printed on the screen:

Just an h1 heading here
Just an h2 heading here
Vue JS is fun
Note that the CodePen app will often update the preview pane even without saving, which is a lot better than refreshing the browser—that must be done when working on your projects locally. Still, it is good to save your CodePen projects often, to not lose any changes (in the odd case of your browser freezing or something else out of the ordinary happening).

Next, let's add the following Vue code to the JS pane inside our pen:

new Vue({
el: '#entryPoint',
data: {
heading1: 'Just an h1 heading here',
heading2: 'heading 2 here',
paragraph1: 'Vue JS'
}
})

Finally, let's update the HTML so that the Vue code can work its magic:

<div id="entryPoint">
<h1>{{ heading1 }}</h1>
<h2>Just an {{ heading2 }}</h2>
<p>{{paragraph1}} is fun</p>
</div>

In the previous code example, we can see how we use mustache templates to dynamically insert data into our HTML.

Mustache templating is achieved by simply passing the keys of our data object into our HTML tags and surrounding the keys with the opening {{ and closing }} tags.

As mentioned before, CodePen will auto-update the preview pane, but this will not affect the preview since we are effectively producing the same output as we did when we were using just plain HTML.

Now we can play with it simply by changing the key-value pairs inside our data entry:

new Vue({
el: '#entryPoint',
data: {
heading1: 'This is an h1',
heading2: 'h2 heading',
paragraph1: 'Vue2'
}
})

This time, the output will auto-update to this:

This is an h1
Just an h2 heading
Vue2 is fun

We can also change our entry point. For example, we can have Vue access only the p tag:

new Vue({
el: 'p',
data: {
heading1: 'This is an h1',
//heading2: 'h2 heading',
paragraph1: 'Vue2'
}
})

After this change, our preview pane will show the following:

{{ heading1 }}
Just an {{ heading2 }}
Vue2 is fun

From this output, we can conclude that our mustache templates will be rendered in our HTML output as regular text if either of the following things happen:

  • Our entry point does not reference the data
  • The entry in our data does not exist

We've also seen how our entry point can be any kind of selector. You can think of it as being similar to how you can target different elements in jQuery.

For example, we could have a more complex selector as our app's entry point:

new Vue({
el: 'div#entryPoint',
data: {
heading1: 'This is an h1',
heading2: 'h2 heading',
paragraph1: 'Vue2'
}
})
主站蜘蛛池模板: 潢川县| 永嘉县| 新竹县| 通山县| 玉树县| 普定县| 达孜县| 浦江县| 江永县| 祁门县| 奎屯市| 丹东市| 大悟县| 拉萨市| 邵武市| 南乐县| 白山市| 永顺县| 扶余县| 延川县| 高清| 乡宁县| 祁东县| 且末县| 嘉兴市| 都江堰市| 临高县| 周至县| 普格县| 柳林县| 嘉义县| 乌拉特前旗| 天镇县| 阿尔山市| 休宁县| 彭山县| 东海县| 高州市| 陕西省| 五家渠市| 巴彦淖尔市|