Vue2.0 的漫长学习ing-2-5

Template 制作模版

直接写在选项里的模板

  直接在构造器里的template选项后边编写。这种写法比较直观,但是如果模板html代码太多,不建议这么写。

js:

 var app=new Vue({
     el:'#app',
     data:{
         message:'hello Vue!'
      },
     template:`
        <h1 style="color:red">我是选项模板</h1>
     `
})

  这里需要注意的是模板的标识不是单引号和双引号,而是,就是Tab上面的键。

写在<template>标签里的模板

    <template id="demo2">
             <h2 style="color:red">我是template标签模板</h2>
    </template>
 
    <script type="text/javascript">
        var app=new Vue({
            el:'#app',
            data:{
                message:'hello Vue!'
            },
            template:'#demo2'
        })
    </script>

写在<srcipt>里面的标签

    <script type="x-template" id="demo3">
        <h2 style="color:red">我是script标签模板</h2>
    </script>
 
    <script type="text/javascript">
        var app=new Vue({
            el:'#app',
            data:{
                message:'hello Vue!'
            },
            template:'#demo3'
        })
    </script>

完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Template三种写法 实例</title>
    <script type="text/javascript" src="../assets/js/vue.js"></script>
</head>
<body>
    <h1>Template三种写法 实例</h1>
    <hr>

    <div id="app">
        {{message}}
    </div>
    <template id="dd2">
        <h2 style="color:red">我是Template标签模板</h2>
    </template>    

    <script type="x-template" id="dd3">
        <h2 style="color:red">我是scrip template标签</h2>
    </script>

    <script type="text/javascript">
        var app = new Vue({
            el: "#app",
            data:{
                
            },
            template:"#dd3"
            // template:` 
            //     <h2 style="color:red">我是选项模板</h2>
            // `
            
            
        })
    </script>
</body>
</html>

写在<script>标签里的模板

猜你喜欢

转载自www.cnblogs.com/xiaofandegeng/p/9003101.html