在编写Vue页面的时候,会碰到这么一个需求。由于页面组件比较多,不可能一次性将所有的页面都打包,只能按需获取相应的页面进行显示。

比如有一个App页面,需要异步获取html片段通过v-html指令加载到content这个组件中。

<div id='app'>
<content v-html='html' />
<input type='button' @click='appendContent' >appendContent</input>
</div> <script type="text/x-template" id='content-template'>
<div class='content' v-html='html'> </div>
</script>

APP JS:

var content = Vue.components('content',{
props:['html'],
template:'#content-template'
}); var app = new Vue({
el:'#app',
data:{
html:''
},
methods:{
appendContent:function() {
$.ajax({
type:'GET',
url:'content.html'
success:function(response){
this.html = response;
}
});
}
}
});

组件

<div id='content'>
<test1 />
<test2 />
</div> <script type="text/x-template" id='test1-template'>
<div>Test 1 Component</div>
</script> <script type="text/x-template" id='test2-template'>
<div>Test 2 Component</div>
</script> <script>
var test1 = {
template:'#test1-template'
}; var test2 = {
template:'#test2-template'
}; var subcontent = new Vue({
el:'#content',
components:{
'test1':test1,
'test2':test2,
}
});
</script>

但是实际执行的时候会发现,组件没能正确渲染。折腾了一通,发现原来v-html指令原来有个坑,插入片段的时候,js代码无法执行

因此需要改变一下执行顺序。

  • 1.将组件拆分成html和js两个文件。
  • 2.先用ajax读取html文件。
  • 3.成功获取html文件后,通过require获取js文件。

修改后的代码如下(注:只是示例,不代表能跑得通):

主界面 html
<div id='app'>
<content v-html='html' />
<input type='button' @click='appendContent' >appendContent</input>
</div> <script type="text/x-template" id='content-template'>
<div class='content' v-html='html'> </div>
</script> 主界面 js var content = Vue.components('content',{
props:['html'],
template:'#content-template'
}); var app = new Vue({
el:'#app',
data:{
html:''
},
methods:{
appendContent:function() {
$.ajax({
type:'GET',
url:'content.html'
success:function(response){
this.html = response;
require(['content'],function(){});
}
});
}
}
}); 组件 html
<div id='content'>
<test1 />
<test2 />
</div> <script type="text/x-template" id='test1-template'>
<div>Test 1 Component</div>
</script> <script type="text/x-template" id='test2-template'>
<div>Test 2 Component</div>
</script> 组件 js <script>
var test1 = {
template:'#test1-template'
}; var test2 = {
template:'#test2-template'
}; var subcontent = new Vue({
el:'#content',
components:{
'test1':test1,
'test2':test2,
}
});
</script>

  

【Vue】动态加载Html片段的更多相关文章

  1. vue动态加载组件

    vue动态加载组件,可以使用以下方式 <component :is="propertyname" v-for="tab in tabs"></ ...

  2. Vue动态加载异步组件

    背景: 目前我们项目都是按组件划分的,然后各个组件之间封装成产品.目前都是采用iframe直接嵌套页面.项目中我们还是会碰到一些通用的组件跟业务之间有通信,这种情况下iframe并不是最好的选择,if ...

  3. vue动态加载图片,取消格式验证

    vue 一. 动态加载图片 (以vue模板为例) app.vue 代码如下: <template> <div id="app"> <img :src= ...

  4. vue实践---vue动态加载组件

    开发中遇到要加载10个或者更多的,类型相同的组件时,如果用普通的 import 引入组件,components注册组件,代码显得太啰嗦了,这时候就需要用到 require.context 动态加载这些 ...

  5. VUE 动态加载组件的四种方式

    动态加载组件的四种方式: 1.使用import导入组件,可以获取到组件 var name = 'system'; var myComponent =() => import('../compon ...

  6. Vue 动态加载组件

    为什么要动态加载呢?而不是一次性加载呢? 一次性?你能保证你拿的内容不多,那从性能方面说还是OK的.否则,就该什么时候用,就什么时候取. 得出这想法,源于前几天上班赶产品的故事: A组件是父亲,B组件 ...

  7. Vue动态加载图片图片不显示

    图片是放在assets文件夹下的 使用require进行解决 图片不显示的原因 在webpack,将图片放在assets中,会将图片图片来当做模块来用,因为是动态加载的,所以url-loader将无法 ...

  8. vue 动态加载图片路径报错解决方法

    最近遇到图片路径加载报错的问题 之前一直都是把图片放到assets的文件下的.总是报错,看到一些文章并且尝试成功了,特意记录下 首先先说明下vue-cli的assets和static的两个文件的区别, ...

  9. vue动态加载图片

    如果是直接动态获取完整的图片地址可以使用以下方法 <template> <img :src="url"> </template> <scr ...

随机推荐

  1. feed.snapdo.com 病毒

    过程:安装破解office2013 使用破解工具 Microsoft toolkit 2.7 beta  1 关闭防火墙 浏览器被木马篡改:搜索引擎被篡改: 相关进程 bittorrent.exe 无 ...

  2. ASP.NET MVC 企业级实战

    1.泛型 public class List<T>{ } 当定义泛型类的实例时,必须指定这个实例所存储的实际类型,泛型允许程序员将一个实际的数据类型规约延迟至泛型的实例被创建时才确定,泛型 ...

  3. Gym - 101982C Contest Setting (动态规划)

    A group of contest writers have written n problems and want to use k of them in an upcoming contest. ...

  4. DTW和DBA

    DTW(动态时间调整) 动态时间调整算法是大多用于检测两条语音的相似程度,由于每次发言,每个字母发音的长短不同,会导致两条语音不会完全的吻合,动态时间调整算法,会对语音进行拉伸或者压缩,使得它们竟可能 ...

  5. 八.django模型系统(二)之常用查询及表关系的实现

    Ⅰ.常用查询  1.几个概念 每一个django模型类,都有一个默认的管理器,objects,查询就是依赖于objects管理器进行的(在创建时就被添加了). QuerySet表示数据库中对象的列表( ...

  6. go语言的开始入门(一)

    前言:作为Web开发人员掌握多种后台是必须的,趁自己有C语言的基础,所以尝试入门Golang (一).基本数据类型的认识 小结:int大小默认与系统相关,byte只能够存单字节,   (二).Gola ...

  7. 实时通讯系列目录篇之SignalR详解

    一. 简单说几句 最早使用SignalR的时候大约是两年前了,记得当时是一个OA中消息的实时提醒,轮询的方式有点耗资源,WebSocket写起来又比较麻烦,最终选择了SignalR,当时是什么版本已经 ...

  8. matlab 加根号

    text(3,0.5,'z=0.2$$\sqrt{c/h_0}$$+0.3','interpreter','latex')

  9. Arduino-常用指令

    pinMode(LEDPin,OUTPUT);        //设置引脚模式 参数1      引脚 参数2      OUTPUT  输出:INPUT   输入 用在setup()函数里 digi ...

  10. arguments.callee.caller

    1.Arguments Arguments是一个类似数组但不是数组的对象,说它类似数组是因为其具有数组一样的访问性质及方式,可以由arguments[n]来访问对应的单个参数的值,并拥有数组长度属性l ...