- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在一个不需要预定义的 HTML 块中的任意点上动态插入新的 vuejs 组件。
这是一个稍微做作的示例,它演示了我正在尝试做的事情:
Vue.component('child', {
// pretend I do something useful
template: '<span>--><slot></slot><--</span>'
})
Vue.component('parent', {
data() {
return {
input: 'lorem',
text: '<p>Lorem ipsum dolor sit amet.</p><p><i>Lorem ipsum!</i></p>'
}
},
template: `<div>
Search: <input type='text' v-model="input"><br>
<hr>
This inserts the child component but doesn't render it
or the HTML:
<div>{{output}}</div>
<hr>
This renders the HTML but of course strips out the child component:
<div v-html="output"></div>
<hr>
(This is the child component, just to show that it's usable here:
<child>hello</child>)
<hr>
This is the goal: it renders both the input html
and the inserted child components:
TODO ¯\_(ツ)_/¯
</div>`,
computed: {
output() {
/* This is the wrong approach; what do I replace it with? */
var out = this.text;
if (this.input) {
this.input = this.input.replace(/[^a-zA-Z\s]/g,'');
var regex = new RegExp(this.input, "gi");
out = out.replace(regex, '<child><b>' + this.input + '</b></child>');
}
return out;
}
}
});
new Vue({
el: '#app'
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.0/vue.js"></script>
<div id="app">
<parent></parent>
</div>
data.text
是经过 sanitizer 的 HTML。
<child>
是一些做一些有用的事情的子组件,我想把它包裹起来
data.text
那些事先不知道的。 (
input
仅用于此处的演示。此 MCVE 与我正在构建的代码并不真正相似,它只是一个示例,显示了我遇到的情况。)
output
函数或父组件的模板,使得来自
input
的 HTML和插入的
<child>
模板是否正确呈现?
$compile
.我正在使用 vuejs2,它删除了 $compile
(出于合理的担忧,它使天真地引入 XSS 漏洞变得太容易了。)output()
的结果到另一个组件中,然后将其用作模板。这似乎是一种很有前途的方法,但我不知道如何更改该辅助组件的模板。 template
只接受一个字符串,而不是像许多其他组件属性那样的函数,所以我不能将模板 html 传递给一个 prop。有点像重写 this.template
内beforeMount()
或 bind()
本来很好,但那里也没有快乐。是否有其他方法可以在安装之前替换组件的模板字符串?template
,我可以将数据传递给组件的 render()
函数...但是我仍然不得不将该 html 字符串解析为嵌套的 createElement
职能。这究竟是什么Vue is doing internally in the first place ;除了自己重新发明之外,有没有什么方法可以解决这个问题?Vue.component('foo', {
props: ['myInput'],
render(createElement) {
console.log(this.myInput); // this works...
// ...but how to parse the html in this.myInput into a usable render function?
// return createElement('div', this.myInput);
},
})
<foo inline-template>{{$parent.output}}</foo>
与普通的旧设备完全相同 {{output}}
.回想起来,这应该是显而易见的,但值得一试。output
到构造函数? (它需要可重复使用不同的输入,多个实例可能同时可见;没有全局变量或单例。)output()
这样荒谬的事情在将要插入的点处将输入拆分为一个数组 <child>
,然后在主模板中做这样的事情: ...
<template v-for="chunk in output">
<span v-html="chunk"></span>
<child>...</child>
</template>
....
这将是可行的,如果费力的话——我也必须将 child 插槽中的内容拆分成一个单独的数组,并在 v-for 期间通过索引获取它,但可以做到... if
input
是纯文本而不是 HTML。在拆分 HTML 时,我经常会在每个
chunk
中得到不平衡的标签。 ,这会在
v-html
时弄乱格式为我重新平衡它。无论如何,整个策略感觉就像一个糟糕的黑客;一定会有更好的办法。
v-html
然后(以某种方式)通过事后 DOM 操作在适当的位置插入子组件?我还没有深入探讨这个选项,因为它也感觉像是一个黑客,和数据驱动策略的相反,但如果所有其他方法都失败了,也许这是一种方法? $compile
中涉及的 XSS 风险- 类似的操作。请放心,我所做的一切都不会以任何方式涉及未经 sanitizer 的用户输入;用户不是插入任意组件代码,而是组件需要在用户定义的位置插入子组件。 Vue.component()
如上面的示例。不会偏离该结构太远的答案是首选,尽管任何有效的方法都会起作用。 Vue.compile()
是一种存在的东西,这是一种我无法相信我错过的东西,如果曾经有过的话。
var precompiled = Vue.compile('<span><child>test</child></span>');
Vue.component('test', {
render: precompiled.render,
staticRenderFns: precompiled.staticRenderFns
});
但是各种尝试将其重新调整为可以接受输入属性的东西都没有成功(例如,以下内容抛出“渲染函数中的错误:ReferenceError:_c 未定义”,我认为是因为
staticRenderFns
还没有准备好
render
什么时候需要它们?
Vue.component('test', {
props: ['input'],
render() { return Vue.compile(this.input).render()},
staticRenderFns() {return Vue.compile(this.input).staticRenderFns()}
});
(这不是因为有两个单独的
compile()
s——在
beforeMount()
内部进行预编译,然后返回其渲染,而 staticRenderFns 会引发相同的错误。)
最佳答案
正如我在上面的评论中提到的,$compile
已被删除,但 Vue.compile
在某些版本中可用。除非在几种情况下,否则我相信您打算使用下面的方法。
Vue.component('child', {
// pretend I do something useful
template: '<span>--><slot></slot><--</span>'
})
Vue.component('parent', {
data() {
return {
input: 'lorem',
text: '<div><p>Lorem ipsum dolor sit amet.</p><p><i>Lorem ipsum!</i></p></div>'
}
},
template: `<div>
Search: <input type='text' v-model="input"><br>
<hr>
<div><component :is="output"></component></div>
</div>`,
computed: {
output() {
if (!this.input)
return Vue.compile(this.text)
/* This is the wrong approach; what do I replace it with? */
var out = this.text;
if (this.input) {
this.input = this.input.replace(/[^a-zA-Z\s]/g,'');
var regex = new RegExp(this.input, "gi");
out = out.replace(regex, '<child><b>' + this.input + '</b></child>');
out = Vue.compile(out)
}
return out;
}
}
});
new Vue({
el: '#app'
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.0/vue.js"></script>
<div id="app">
<parent></parent>
</div>
component
接受编译输出的结果。
text
不是有效模板,因为它有多个根。我添加了一个包装
div
使其成为有效的模板。
text
中的任何 HTML 标签的全部或部分匹配,这将失败。 .例如,如果您输入“i”、“di”或“p”,结果将不是您所期望的,并且某些组合会在编译时引发错误。
关于vuejs2 - 在 vuejs2 数据中动态插入子组件(不使用 $compile 或滥用 v-html),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44369839/
我在运行 compile test:compile it:compile经常并且...希望将击键次数减少到类似 *:compile 的数量。 .不过,它似乎不起作用。 $ sbt *:compile
有人可以给我这个问题的提示(或整个解决方案!): 在 Clojurescript 项目中,如何自动将编译日期/时间硬编码在符号中,以便在使用应用程序时显示? 谢谢。 最佳答案 有多种解决方案: 使用l
我是 ember.js 框架的新手,使用 ruby on rails 和 ember.debug.js -v 1.10.1(最新版本)。我一直在网上看到 ember 更改了这个最新的补丁,但我不知
我不是 Fortran 程序员(只是短暂的经验),但我需要编译一个部分用 F77 编写的程序。在我之前有人用 Absoft 编译器编译过它,但现在我需要在另一台机器上用 g77 重复这个过程。对于 A
我运行命令 mvn clean package 我得到了上面的错误我的 pom 是: http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
我有以下问题。 我想在测试编译阶段排除一些.java文件(** / jsfunit / *。java),另一方面,我想在编译阶段包括它们(id我使用tomcat启动tomcat:运行目标) ) 我的p
符合 wikipedia A compiler is a computer program (or set of programs) that transforms source code writt
我想构建项目,但出现如下错误: 无法执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile
当我通过右键单击项目名称进行 Maven 安装时,出现以下错误: [INFO] Scanning for projects... [WARNING] [WARNING] Some proble
我是 Maven 的新手,我想将我的应用程序导入到 Maven。和以前一样,我想将我的 ejb 项目中的类引用到我的 war 项目中。我在类中没有错误,但是如果我在我的父项目上安装 maven,那么我
当我将 ASP.NET Web 应用程序部署到生产环境时,我使用配置转换来删除 debug="true"来自 .但是,就在今天,我注意到 web.config 中的另一个部分如下所示:
This question already has answers here: Maven Compilation Error: (use -source 7 or higher to enable
我正在使用 Maven 3.0.5 和 Spring Tool Source 3.2 并安装了 Maven 插件。当我尝试执行“运行方式---> Maven 安装”时,出现以下错误: [INFO] S
我试图用 AngularJS 创建我自己的递归指令,它调用自己以漂亮的 JSON 格式转换 View 中的对象。好吧,首先我使用 ng-include 调用带有模板的脚本,在其中使用 ng-if 验证
可以通过 @suppress annotation使用Google的Closure Compiler在每个文件的基础上禁止显示警告。但是,似乎无法同时抑制多个警告-例如globalThis和check
假设一个拥有 10 到 20 年经验的熟练开发人员从未构建过编译器或模拟器,哪一个会更具挑战性? 你能比较一下会成为障碍的问题吗? 谢谢。 最佳答案 仿真和编译是完全不同的,但由于两者都被认为是“低级
最近发现Vim中有一个命令叫compiler。您可以使用任何常见的编译器(例如,:compiler gcc、:compiler php 等)来调用它,但它似乎没有任何立竿见影的效果。 我在联机帮助页上
我试图从 spring.io 指南中部署最简单的应用程序 Guide 但是我有一些麻烦.. 我做了什么: 创建的项目。 (来自 spring.io 教程) 下载 heroku CLI 在 Intell
每当进行 Maven Build..>clean install 时,我都会遇到此错误。我尝试过使用不同版本的插件并添加 testFailureIgnore 属性,但问题仍然存在。请找到下面的 POM
我有一个 web 应用程序,我尝试使用 maven 进行编译,不幸的是,在执行 mvn clean package 时它不起作用。 stackoverflow 上有很多问题看起来都一样,但没有解决了我
我是一名优秀的程序员,十分优秀!