Open
Description
I want use it in vue with its directive with hightlightjs, how to use it?
Vue.directive('highlightjs', {
deep: true,
bind: function(el, binding) {
// on first bind, highlight all targets
let targets = el.querySelectorAll('code')
targets.forEach((target) => {
// if a value is directly assigned to the directive, use this
// instead of the element content.
if (binding.value) {
target.textContent = binding.value
console.log(target)
}
hljs.highlightBlock(target)
// hljs.lineNumbersBlock(target)
})
},
componentUpdated: function(el, binding) {
// after an update, re-fill the content and then highlight
let targets = el.querySelectorAll('code')
targets.forEach((target) => {
if (binding.value) {
target.textContent = binding.value
// hljs.initLineNumbersOnLoad();
hljs.highlightBlock(target)
// hljs.lineNumbersBlock(target)
}
})
}
})
Activity
wcoder commentedon Feb 27, 2020
I haven't experience in vue.js, but found some solution for you in vue-highlightjs package:
https://github.com/metachris/vue-highlightjs/blob/master/index.js#L8
To continue will be the same as highlightjs,
Usage - initialization: https://github.com/wcoder/highlightjs-line-numbers.js#usage
xiaodun commentedon Mar 6, 2020
xiaodun commentedon Mar 6, 2020
but the format is invalid!
tanganyu1114 commentedon Nov 26, 2020
@xiaodun 这个问题你找到解决的办法了吗?遇到相同的问题了,求告知,谢谢
Mirocos commentedon Nov 26, 2020
I import highlight.js and highlightjs-line-numbers.js in index.html. Then use following code to initialize:
when it comes to update the highlight code and just use following code to re-render it:
The html code is here:
tanganyu1114 commentedon Nov 26, 2020
I try it ,but failed o(╥﹏╥)o , Thanks all the way
Mirocos commentedon Nov 26, 2020
where did you initialize the format?
Nothing-bit commentedon Apr 7, 2022
i have solved this problem, and i've recorded it in my blog https://www.zhoujianguo.ltd/#/fore/article?id=140
hywhuangyuwei commentedon Nov 28, 2022
My Vue3 + TypeScript + "vue-plugin-load-script" package solution:
em-heqian commentedon Apr 28, 2025
try this #98 (comment)