반응형
컴포넌트로 랩된html을 캡처하여 vue.js의 데이터 값을 설정합니다.
나는 팁 기반의 컴포넌트를 만들고 있다.컴포넌트 태그 내에 있는html을 가져오고 싶다.
<vue-wysiwyg>
<p>Hey, this is some test text.</p>
<p>I should end up in editor.content</p>
</vue-wysiwyg>
그리고 내 컴포넌트에 대한 editor.content data를 보내달라고 해.
export default {
data() {
return {
editor: new Editor({
content: '',
}),
}
},
}
슬롯에 대해 생각해 봤는데 html을 통과했을 뿐 캡처가 되지 않습니다.
슬롯에 vdom 객체가 포함되어 있기 때문에 vue를 사용하여 html로 변환해야 합니다.다음 기능이 작동합니다.
computed: {
html(){
return new Vue({
render: h => h('div', {}, this.$slots.default)
})
.$mount().$el.innerHTML
}
}
렌더링 기능만 있는 단순한 컴포넌트 인스턴스를 만듭니다.$mount
DOM을 생성하기 위해서입니다.innerHTML
root 요소에서 추출합니다.
메모: 이 파일의 Vue를 Import해야 합니다.
컴포넌트에 a를 지정할 수 있습니다.ref
내부 HTML에 접속하여 할당한다.editor.content
<vue-wysiwyg ref="wysiwyg">
<p>Hey, this is some test text.</p>
<p>I should end up in editor.content</p>
</vue-wysiwyg>
그리고 장착된 라이프 사이클 훅은
export default {
data() {
return {
editor: new Editor({
content: '',
}),
}
},
mounted() {
this.editor.content = this.$refs.wysiwyg.$el.innerHTML;
}
}
도움이 되었으면 좋겠다
언급URL : https://stackoverflow.com/questions/58253943/capturing-html-wrapped-by-the-component-to-set-a-data-value-in-vue-js
반응형
'sourcecode' 카테고리의 다른 글
express 및 vue js를 사용하여 로그인 성공 후 특정 페이지로 리디렉션하는 방법 (0) | 2022.09.03 |
---|---|
Java에서는 스레드가 실행 중인지 어떻게 판단합니까? (0) | 2022.09.03 |
"MVC"의 "컨트롤러"에 들어가는 내용은 무엇입니까? (0) | 2022.08.31 |
Linux에서의 낮은 memcpy 퍼포먼스 (0) | 2022.08.31 |
Java: 0 <= x < n 범위의 난수 (0) | 2022.08.31 |