admin管理员组文章数量:1026989
I receive some text from server which may contain some hashtags and when displaying this text I would like to convert these tags with links.
Example text is: "Today #weather is very nice"
Following code converts the string to
Today <router-link to="/tag/weather">#weather</router-link> is very nice
but it is not rendered again to <a>
tag.
<template>
<p v-html="content"/>
</template>
<script>
export default {
methods: {
convertHashTags: function(str) {
return str.replace(/#([\w]+)/g,'<router-link to="/tag/$1">#$1</router-link>')
}
},
data() {
return{
content: 'Today #weather is very nice'
};
}
</script>
How can I re-render content?
I tried this approach but it expects whole string to be a single link not some text and some links.
I receive some text from server which may contain some hashtags and when displaying this text I would like to convert these tags with links.
Example text is: "Today #weather is very nice"
Following code converts the string to
Today <router-link to="/tag/weather">#weather</router-link> is very nice
but it is not rendered again to <a>
tag.
<template>
<p v-html="content"/>
</template>
<script>
export default {
methods: {
convertHashTags: function(str) {
return str.replace(/#([\w]+)/g,'<router-link to="/tag/$1">#$1</router-link>')
}
},
data() {
return{
content: 'Today #weather is very nice'
};
}
</script>
How can I re-render content?
I tried https://codepen.io/movii/pen/WORoyg this approach but it expects whole string to be a single link not some text and some links.
Share Improve this question edited Nov 13, 2017 at 23:38 Employed Russian 215k36 gold badges321 silver badges389 bronze badges asked Nov 13, 2017 at 21:45 Gokhan CelikkayaGokhan Celikkaya 7661 gold badge8 silver badges22 bronze badges1 Answer
Reset to default 5Your code seems to fit ok into the dynamic-link ponent.
console.clear()
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
const Weather = { template: '<div>weather</div>' }
const routes = [
{ path: '/foo', ponent: Foo },
{ path: '/bar', ponent: Bar },
{ path: '/tag/weather', ponent: Weather },
]
Vue.ponent('dynamic-link', {
template: '<ponent v-bind:is="transformed"></ponent>',
props: ['text'],
methods: {
convertHashTags: function(str) {
const spanned = `<span>${str}</span>`
return spanned.replace(/#([\w]+)/g,'<router-link to="/tag/$1">#$1</router-link>')
}
},
puted: {
transformed () {
const template = this.convertHashTags(this.text);
return {
template: template,
props: this.$options.props
}
}
}
})
const router = new VueRouter({ routes })
const app = new Vue({
router,
data () {
return {
text: 'Today #weather is very nice'
}
}
}).$mount('#app');
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.1.10/vue.min.js"></script>
<script src="https://unpkg./vue-router/dist/vue-router.js"></script>
<div id="app">
<router-link to="/bar">Go to Bar</router-link> |
<dynamic-link :text="text"></dynamic-link>
<router-view></router-view>
</div>
I receive some text from server which may contain some hashtags and when displaying this text I would like to convert these tags with links.
Example text is: "Today #weather is very nice"
Following code converts the string to
Today <router-link to="/tag/weather">#weather</router-link> is very nice
but it is not rendered again to <a>
tag.
<template>
<p v-html="content"/>
</template>
<script>
export default {
methods: {
convertHashTags: function(str) {
return str.replace(/#([\w]+)/g,'<router-link to="/tag/$1">#$1</router-link>')
}
},
data() {
return{
content: 'Today #weather is very nice'
};
}
</script>
How can I re-render content?
I tried this approach but it expects whole string to be a single link not some text and some links.
I receive some text from server which may contain some hashtags and when displaying this text I would like to convert these tags with links.
Example text is: "Today #weather is very nice"
Following code converts the string to
Today <router-link to="/tag/weather">#weather</router-link> is very nice
but it is not rendered again to <a>
tag.
<template>
<p v-html="content"/>
</template>
<script>
export default {
methods: {
convertHashTags: function(str) {
return str.replace(/#([\w]+)/g,'<router-link to="/tag/$1">#$1</router-link>')
}
},
data() {
return{
content: 'Today #weather is very nice'
};
}
</script>
How can I re-render content?
I tried https://codepen.io/movii/pen/WORoyg this approach but it expects whole string to be a single link not some text and some links.
Share Improve this question edited Nov 13, 2017 at 23:38 Employed Russian 215k36 gold badges321 silver badges389 bronze badges asked Nov 13, 2017 at 21:45 Gokhan CelikkayaGokhan Celikkaya 7661 gold badge8 silver badges22 bronze badges1 Answer
Reset to default 5Your code seems to fit ok into the dynamic-link ponent.
console.clear()
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
const Weather = { template: '<div>weather</div>' }
const routes = [
{ path: '/foo', ponent: Foo },
{ path: '/bar', ponent: Bar },
{ path: '/tag/weather', ponent: Weather },
]
Vue.ponent('dynamic-link', {
template: '<ponent v-bind:is="transformed"></ponent>',
props: ['text'],
methods: {
convertHashTags: function(str) {
const spanned = `<span>${str}</span>`
return spanned.replace(/#([\w]+)/g,'<router-link to="/tag/$1">#$1</router-link>')
}
},
puted: {
transformed () {
const template = this.convertHashTags(this.text);
return {
template: template,
props: this.$options.props
}
}
}
})
const router = new VueRouter({ routes })
const app = new Vue({
router,
data () {
return {
text: 'Today #weather is very nice'
}
}
}).$mount('#app');
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.1.10/vue.min.js"></script>
<script src="https://unpkg./vue-router/dist/vue-router.js"></script>
<div id="app">
<router-link to="/bar">Go to Bar</router-link> |
<dynamic-link :text="text"></dynamic-link>
<router-view></router-view>
</div>
本文标签: javascriptVuejs replace hashtags in text with ltrouterlinkgtStack Overflow
版权声明:本文标题:javascript - Vue.js replace hashtags in text with <router-link> - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745657821a2161702.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论