var Toast={};
Toast.install = function (Vue, options) {
let opt = {
defaultType:'bottom', // 默认显示位置
duration:'2500' // 持续时间
}
if(options){
console.log(JSON.stringify(options))
for(let property in options){
opt[property] = options[property]; // 使用 options 的配置
}
}
// 1. 添加全局方法或属性
Vue.prototype.toast = function (tips) {
if(document.getElementsByClassName('vue-toast').length){
// 如果toast还在,则不再执行
return;
}
let toastTpl = Vue.extend({
template: '<div class="vue-toast toast-'+opt.defaultType+'">' + tips + '</div>'
});
let tpl = new toastTpl().$mount().$el;
document.body.appendChild(tpl);
setTimeout(function () {
document.body.removeChild(tpl);
}, opt.duration)
}
}
export default Toast;

  

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

(found in <Root>)

main.js中:

import Toast from '@/utils/toast/'
Vue.use(Toast,{aa:'1',bb:'---'})
 
某处:this.toast("11111")

不行XXXXXXXXX啊。这个写法只适用于那种不需要编译,runtine.vue.js  这种。

解决方法一:

var Toast={};
Toast.install = function (Vue, options) {
let opt = {
defaultType:'bottom', // 默认显示位置
duration:'2500' // 持续时间
}
if(options){
console.log(JSON.stringify(options))
for(let property in options){
opt[property] = options[property]; // 使用 options 的配置
}
}
// 1. 添加全局方法或属性
Vue.prototype.toast = function (tips) {
if(document.getElementsByClassName('vue-toast').length){
// 如果toast还在,则不再执行
return;
} // let toastTpl = Vue.extend({
// template: '<div class="vue-toast toast-'+opt.defaultType+'">' + tips + '</div>'
// });
// let tpl = new toastTpl().$mount().$el; let toastTpl = new Vue({
render (h) {
return h('div', tips)
}
})
let tpl = toastTpl.$mount().$el;
document.body.appendChild(tpl);
setTimeout(function () {
document.body.removeChild(tpl);
}, opt.duration)
}
}
export default Toast;

  能解决,但是添加css不方便,所以另外想办法。

最后还是用这种方法:

var Toast={};
Toast.install = function (Vue, options) {
let opt = {
defaultType:'bottom', // 默认显示位置
duration:'2500' // 持续时间
}
if(options){
console.log(JSON.stringify(options))
for(let property in options){
opt[property] = options[property]; // 使用 options 的配置
}
}
// 1. 添加全局方法或属性
Vue.prototype.toast = function (tips) {
if(document.getElementsByClassName('vue-toast').length){
// 如果toast还在,则不再执行
return;
} /**
* 需要编译器
*/
// let toastTpl = Vue.extend({
// template: '<div class="vue-toast toast-'+opt.defaultType+'">' + tips + '</div>'
// });
// let tpl = new toastTpl().$mount().$el; /**
* 不需要编译器
*/ /**
* 对不同构建版本的解释:
* 完整版:同时包含编译器和运行时的版本。完整版 vue.js vue.common.js vue.esm.js
* 运行时:用来创建 Vue 实例、渲染并处理虚拟 DOM 等的代码。基本上就是除去编译器的其它一切。只包含运行时版 vue.runtime.js vue.runtime.common.js vue.runtime.esm.js
* 编译器:用来将模板字符串编译成为 JavaScript 渲染函数的代码。
* https://cn.vuejs.org/v2/guide/installation.html#%E8%BF%90%E8%A1%8C%E6%97%B6-%E7%BC%96%E8%AF%91%E5%99%A8-vs-%E5%8F%AA%E5%8C%85%E5%90%AB%E8%BF%90%E8%A1%8C%E6%97%B6
* 运行时 + 编译器 vs. 只包含运行时
如果你需要在客户端编译模板 (比如传入一个字符串给 template 选项,或挂载到一个元素上并以其 DOM 内部的 HTML 作为模板),就将需要加上编译器,即完整版: // 需要编译器
new Vue({
template: '<div>{{ hi }}</div>'
}) // 不需要编译器
new Vue({
render (h) {
return h('div', this.hi)
}
})
当使用 vue-loader 或 vueify 的时候,*.vue 文件内部的模板会在构建时预编译成 JavaScript。你在最终打好的包里实际上是不需要编译器的,所以只用运行时版本即可。 因为运行时版本相比完整版体积要小大约 30%,所以应该尽可能使用这个版本。如果你仍然希望使用完整版,则需要在打包工具里配置一个别名: webpack
module.exports = {
// ...
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js' // 用 webpack 1 时需用 'vue/dist/vue.common.js'
}
}
}
*/
let toastTpl = new Vue({
render (createElement) {
if (tips) {
return createElement('div',{
style:{
position: 'fixed',
top: '0',
left: '0',
height: '100%',
width: '100%',
},
},[
createElement('div', {
props:{icon:'search'},
style:{
backgroundColor:'red',
border: 'none',
fontSize: '21px',
margin: '0px 10px 6px 0px',
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translateX(-50%) translateY(-50%)',
},
on:{
click:()=>{
console.log('fff')
}
}
},tips)
])
}
}
})
let tpl = toastTpl.$mount().$el;
document.body.appendChild(tpl);
setTimeout(function () {
document.body.removeChild(tpl);
}, opt.duration)
}
}
export default Toast;

  https://cn.vuejs.org/v2/guide/render-function.html#%E5%AE%8C%E6%95%B4%E7%A4%BA%E4%BE%8B

https://cn.vuejs.org/v2/guide/installation.html#%E8%BF%90%E8%A1%8C%E6%97%B6-%E7%BC%96%E8%AF%91%E5%99%A8-vs-%E5%8F%AA%E5%8C%85%E5%90%AB%E8%BF%90%E8%A1%8C%E6%97%B6

好好看看吧

vue小toast插件报错runtine-only的更多相关文章

  1. maven插件报错之解决

    maven插件报错之解决 用m2eclipse创建Maven项目时报错 maveneclipsebuilddependenciesauthorizationplugins 用m2eclipse创建 ...

  2. dojo表格分页插件报错

    dojo表格分页插件报错 (1)dojo/parser::parse() error ReferenceError {stack:(...),message:"layout is not d ...

  3. DataTable插件报错:Uncaught TypeError: Cannot read property 'style' of undefined

    DataTable插件报错:Uncaught TypeError: Cannot read property 'style' of undefined 原因:table 中定义的列和aoColumns ...

  4. vue init webpack nameXXX 报错问题:

    vue新建demo项目报错如下: M:\lhhVueTest>vue init webpack L21_VueProject vue-cli · Failed to download repo ...

  5. sublime 安装插件报错

    sublime  安装插件报错,大部分原因是本地防火墙开启了,关闭本地防火墙

  6. vscode安装dlv插件报错:There is no tracking information for the current branch.

    vscode安装dlv插件报错:There is no tracking information for the current branch. https://blog.csdn.net/a7859 ...

  7. The command ("dfs.browser.action.delete") is undefined 解决Hadoop Eclipse插件报错

    Hadoop Eclipse插件 报错. 使用 hadoop-eclipse-kepler-plugin-2.2.0.jar 如下所示 Error Log 强迫症看了 受不了 The command ...

  8. 01-路由跳转 安装less this.$router.replace(path) 解决vue/cli3.0语法报错问题

    2==解决vue2.0里面控制台包的一些语法错误. https://www.jianshu.com/p/5e0a1541418b 在build==>webpack.base.conf.j下注释掉 ...

  9. vue 表单校验报错 "Error: please transfer a valid prop path to form item!"

    vue 表单校验报错 "Error: please transfer a valid prop path to form item!" 原因:prop的内容和rules中定义的名称 ...

随机推荐

  1. Java精选笔记_面向对象(慨念、类和对象)

    面向对象概念 在程序中使用对象来映射现实中的事物,使用对象的关系来描述事物之间的联系,这种思想就是面向对象. 相对于面向过程而言.是一种思想,强调的是功能的对象. 面向对象的本质:以类的方式组织代码, ...

  2. python2.0_s12_day9_事件驱动编程&异步IO

    论事件驱动与异步IO 事件驱动编程是一种编程范式,这里程序的执行流由外部事件来决定.它的特点是包含一个事件循环,当外部事件发生时使用回调机制来触发相应的处理.另外两种常见的编程范式是(单线程)同步以及 ...

  3. Python 数据类型:元组

    一.元组介绍 1. 元组可以存储一系列的值,使用括号来定义,每个元素之间用逗号隔开,形如 ('a', 'b', 'c', 'd')2. 元组一旦定义,元组内的元素就不能再修改,除非重新定义一个新的元组 ...

  4. 《C++ Primer Plus》15.4 RTTI 学习笔记

    运行时类型识别RTTI(Runtime Type Identification) C++有三个支持RTTI的元素.* 如果可能的话,dynamic_cast运算符将使用一个指向基类的指针来生成一个指向 ...

  5. C++异常 调用abort()

    以一个计算两个数的调和平均数的函数为例.两个数的调和平均数的定义是:这两个数倒数的平均值的倒数,因此表达式为:1.0 * x * y / (x + y)如果y是x的负值,则上述公式将导致被零除——一种 ...

  6. Django restframwork

    REST介绍 全称Representational State Transfer,即表现层状态转换,如果一个架构符合REST原则,我们就称他为Restfull架构,其主要包括如下方面: 资源Resou ...

  7. vux报错二

    执行npm run build后 "build": "node build/build.js",   // 输出提示信息 - 提示用户请在 http 服务下查看 ...

  8. koan重装system

    author:headsen chen date: 2018-08-02   16:29:51 koan是kickstart-over-a-network的缩写,它是cobbler的客户端帮助程序,k ...

  9. sping整合quartz

    很简单,一共需要定义三个bean 需要注意的是每个bean的类型 业务bean(就是我们每次调度需要做的工作) <bean id="quantzjobBean" class= ...

  10. Unity3D笔记七 GUILayout

    一.说到GUILayout就要提到GUI,二者的区别是什么 GUILayout是游戏界面的布局.GUI(界面)和GUILayout(界面布局)功能上面是相似的从命名中就可以看到这两个东西非常相像,但是 ...