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

我希望把 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>

 

https://blog.csdn.net/xiaodi520520/article/details/88785676      这个应该能帮到你

<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的代码

<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>