leeke_zhou
leeke_zhou
采纳率64.3%
2021-02-20 13:18

vue.js 初学者关于把一段template写进js的问题

5
已采纳

我希望把 id为“vue_id” 做成一个template 并写在 下方的javascript里面,该如何实现?

 

<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> 
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>

<body>
<!--如何把 id为“vue_id” 做成一个template 并写在 下方的javascript里面--> 
<!-- Template: Start -->
<div class="text-center" id="vue_id">
  <div> <a v-for="item in model" v-bind:href="'#'+item.href_value" class="btn btn-primary mrs mbs">{{ item.text_value }}</a></div>
</div>
<!-- Template: End -->
</body>
<script>
	var app2 = new Vue({
    el: '#vue_id',
		// 想要把template写在这里
    data: {
        test: 'test',
        model: []
    },
    mounted: function() {

        axios.get('mark_0220150514.json')
            .then(response => {
                this.model = response.data;
                console.log(response);
            })
            .catch(error => {
                // handle error
                console.log(error);
            })
    }
});
	</script>
</html>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • hu071700 AllPromise 3月前
    点赞 1 评论 复制链接分享
  • leewen5 leewen5 23天前
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> 
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    </head>
     
    <body>
    <div id="app"></div>
    </body>
    <script>
    	var app2 = new Vue({
        el: '#app',
        template: `<div class="text-center" id="vue_id">
      <div> <a v-for="item in model" v-bind:href="'#'+item.href_value" class="btn btn-primary mrs mbs">{{ item.text_value }}</a></div>
    </div>`	// 把template写在这里
        data: {
            test: 'test',
            model: []
        },
        mounted: function() {
     
            axios.get('mark_0220150514.json')
                .then(response => {
                    this.model = response.data;
                    console.log(response);
                })
                .catch(error => {
                    // handle error
                    console.log(error);
                })
        }
    });
    	</script>
    </html>
    点赞 评论 复制链接分享
  • a982357361 a982357361 3月前
    <template>
      <div id="app">
        <div v-html="htmlValue"></div>
      </div>
    </template>
    
    <script>
    export default {
      data(){
        return{
          htmlValue:"<h1 style='color:red'>我是h1</h1>"
        }
      }
    }
    </script>

    用v-html来绑变量就可以了,这样变量里可以直接写html的代码

    点赞 评论 复制链接分享

相关推荐