Runtime Only和Runtime + Compiler

如果你需要在客户端编译模板 (比如传入一个字符串给 template 选项,或挂载到一个元素上并以其 DOM 内部的 HTML 作为模板),就将需要加上编译器,即完整版

当使用 vue-loader 或 vueify 的时候,*.vue 文件内部的模板会在构建时预编译成 JavaScript。你在最终打好的包里实际上是不需要编译器的,所以只用运行时版本即可— 官方文档

客户端编译模板

 1   <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
 2
 3   <div id="app"></div>
 4
 5   <script>
 6     new Vue({
 7       el: ‘#app‘,
 8       data: {
 9         message: ‘Hello Vue!‘
10       },
11       template: ‘<div>{{ message }}</div>‘
12     })
13   </script>

这种用法就需要在客户端(即浏览器中)编译模板,模版的内容是 <div>{{ message }}</div> ,模版的数据是 message: ‘Hello Vue!‘ 。

因此,如果使用 script 引入只有运行时版本的vue.js vue.runtime.js ,就会报错:

vue.runtime.js:593 [Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.

使用渲染函数

 1   <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.runtime.js"></script>
 2
 3   <div id="app"></div>
 4
 5   <script>
 6     new Vue({
 7       el: ‘#app‘,
 8       data: {
 9         message: ‘Hello Vue!‘
10       },
11       render (createElement) {
12         return createElement(‘div‘, this.message)
13       }
14     })
15   </script>

这种用法就是直接给出渲染函数来进行内容输出(具体 createElement 语法后面再讲),这种情况下不需要进行客户端渲染,直接引用运行时版本的vue.js即可,并不会报错。

预编译模板

简单的页面结构我们可以直接通过 createElement 函数来手动编写,但是对于复杂的页面结构呢?vue提供了 Vue.compile 函数用于将模版编译成 render 函数,示例如下:

1 <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
2
3   <script>
4     const result = Vue.compile(‘<div>{{ message }}</div>‘);
5     console.log(result.render);
6   </script>

注意:只有 Runtime + Compiler 版本的vuejs才有 Vue.compile 函数,运行时版本的vue.js是没有这个函数的,所以这里也就是所谓的预编译。

输出的结果如下:

1 ƒ anonymous(
2 ) {
3 with(this){return _c(‘div‘,[_v(_s(message))])}
4 }

我们用预编译生成的render函数替代上一章的 render 函数:

 1  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.runtime.js"></script>
 2
 3   <div id="app"></div>
 4
 5   <script>
 6     new Vue({
 7       el: ‘#app‘,
 8       data: {
 9         message: ‘Hello Vue!‘
10       },
11       render (createElement) {
12         with(this){return _c(‘div‘,[_v(_s(message))])}
13       }
14     })
15   </script>

可以看到这种“开发时预编译,上线使用运行时”的方式既满足了开发需要又减少了线上文件的大小。

注:通过打印,可以看到 createElement 以及 this._c ,  this._v  和  this._s 的值:

1  render (createElement) {
2         console.log(createElement, this._c, this._v, this._s)
3         with(this){return _c(‘div‘,[_v(_s(message))])}
4       }
5 ƒ (a, b, c, d) { return createElement(vm, a, b, c, d, true); }
6 ƒ (a, b, c, d) { return createElement(vm, a, b, c, d, false); }
7 ƒ createTextVNode (val) {...}
8 ƒ toString (val) {...}

所以可以看出,我们最开始手写的渲染函数 return createElement(‘div‘, this.message) 只是上面预编译生成的一个简版。

基于 HTML 的模板语法

除了上面几种基于js代码的形式来创建模版,vuejs也支持基于 HTML 的模板语法,用法如下:

 1   <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
 2 </head>
 3 <body>
 4   <div id="app"><div>{{ message }}</div></div>
 5
 6   <script>
 7     new Vue({
 8       el: ‘#app‘,
 9       data: {
10         message: ‘Hello Vue!‘
11       }
12     })
13   </script>

注意,这种写法也必须要使用 Runtime + Compiler 版本的vuejs才可以允许,因为其实质上还是在客户端进行模版编译,因为上面的写法实质上等同于下面的写法:

 1   <div id="app"></div>
 2   <template id="tpl">
 3     <div>{{ message }}</div>
 4   </template>
 5
 6   <script>
 7     new Vue({
 8       el: ‘#app‘,
 9       data: {
10         message: ‘Hello Vue!‘
11       },
12       template: ‘#tpl‘
13     })
14   </script>

vuejs源码中的 template 解析

在vuejs源码中,关于获取 template 的关键代码如下:

 1    var template = options.template;
 2     if (template) {
 3       if (typeof template === ‘string‘) {
 4         if (template.charAt(0) === ‘#‘) {
 5           template = idToTemplate(template);
 6         }
 7       } else if (template.nodeType) {
 8         template = template.innerHTML;
 9       } else {
10         {
11           warn(‘invalid template option:‘ + template, this);
12         }
13         return this
14       }
15     } else if (el) {
16       template = getOuterHTML(el);
17     }

逻辑主要步骤如下:

  • 先判断是否有 template 属性
  • 如果没有,则直接通过 el 中的 html 代码作为模版
  • 如果有,判断是否是字符串(非字符串的形式暂不讨论)
  • 是字符串的情况下,是否以#字符开头
  • 如果是,则获取对应id的 innerHTML 作为模版
  • 如果不是以#字符开头,则直接作为作为模版

总结

一句话来讲就是:如果有 render 函数就可以使用运行时版本的vuejs,否则必须使用 Runtime + Compiler 版本的vuejs。

原文地址

原文地址:https://www.cnblogs.com/joyco773/p/11657503.html

时间: 2024-08-02 08:40:12

Runtime Only和Runtime + Compiler的相关文章

Target runtime com.genuitec.runtime.generic.jee60 is not defined

转载自:http://jingyan.baidu.com/article/d7130635338e3f13fdf47518.html 用eclipse加载别人的工程,报错Target runtime com.genuitec.runtime.generic.jee60 is not defined 系统加载工程后,报错Target runtime com.genuitec.runtime.generic.jee60 is not defined,在发布工程的同事电脑上正常 新导入的工程,出问题很

用eclipse加载别人的工程,报错Target runtime com.genuitec.runtime.generic.jee60 is not defined

系统加载工程后,报错Target runtime com.genuitec.runtime.generic.jee60 is not defined,在发布工程的同事电脑上正常 新导入的工程,出问题很大可能是jdk的版本问题导致,检查一下,发现jdk果然不一致,修改了jdk版本,但异常没有消除 网上查询下解决方案,原来在工程目录下的settings,有个文件也需要修改下 打开该文件,会发现<runtime name="com.genuitec.runtime.generic.jee60&q

《Objective-C Runtime分析(一)-Runtime初步》

http://www.tekuba.net/program/335/ 本系列主要参考资料:Objective-C Runtime ReferenceObjective-C Runtime Programming Guide涉及主要文件:objc/message.h,objc/objc-api.h,objc/objc.h,objc/runtime.h特酷吧[tekuba.net]采用"署名-非商业用途-保持一致"的创作共用协议,使用本文内容请遵循该协议 Objective-C Runti

DescriptionResourcePathLocationType Target runtime com.genuitec.runtime.generic.jee60 is not defined.project2UnknownFace 解决方案

[1]import导入以前的项目,项目上有一个红叉,其他的地方没有错误,但不影响操作. 报错信息:Description Resource Path Location Type Target runtime com.genuitec.runtime.generic.jee60 is not defined. project2 Unknown Faceted Project Problem 解决办法: 在本地找到工程目录下的.settings下的org.eclipse.wst.common.pro

Target runtime com.genuitec.runtime.generic.jee50 is not defined工程错误

导入别人的工程,发现报错Target runtime com.genuitec.runtime.generic.jee50 is not defined 解决方法:1. 找到工程目录的.settings2. 找到org.eclisep.wst.common.project.facet.core.xml文件 3. 打开该文件,会发现<runtime name="com.genuitec.runtime.generic.jee60" />,把这句话去掉4. 刷新工程就行了 原文

iOS开发runtime学习:一:runtime简介与runtime的消息机制

一:runtime简介:也是面试必须会回答的部分 二:runtime的消息机制 #import "ViewController.h" #import <objc/message.h> #import "Person.h" /* 总结: 1: runtime:必须要导入头文件 <objc/message.h>,此头文件中已经引入了<objc/runtime.h> 任何方法调用本质:发送一个消息,用runtime发送消息.OC底层实现

RunTime(消息机制) + RunTime(消息转发)

一.消息机制 1.在viewDidLoad中直接用 performSelector:@selector(doSomething) 来调用doSomething方法时,会发现找不到这个方法而奔溃.此时,我们可以在resolveInsantanceMethod:(SEL)see 方法中获取这个所有在运行时阶段的方法,在这个方法中只需要判断一下,将这个方法获取,并且运用Runtime 的 class_addMethod 的方法来将方法和响应函数绑定,进而达到为某一个类添加方法的目的. - (void)

target runtime com.genuitec.runtime.genuitec.jee60 is not defined

选中项目,右键 -> Properties -> Project Facets -> 在Runtimes 里 选择用Tomcat运行,然后 Apply -> OK. 问题解决. 原文地址:https://www.cnblogs.com/rongyao/p/9346449.html

iOS runtime和runloop

runtime 和 runloop 作为一个程序员进阶是必须的,也是非常重要的, 在面试过程中是经常会被问到的, 所以大家有必要进行研究,有能力的童鞋可以和下面作者一样, 亲历实践一下. 在简书里发现了两篇非常好的文章介绍 runtime和runloop的,在这里合二为一了, 把原版作者的东西拿了过来, 为了尊重作者,在这里注明一下 @sam_lau 是runtime的作者, @tripleCC是runloop的作者   RunTime Objective-C是基于C语言加入了面向对象特性和消息