vue模版字符串解析成vue模版对象

模版字符串

this.code= `
<template>
    <div style="width:100% ; height: 100% ;">
        {{resultData[0].name}}
    </div>
</template>
<script>
export default {
    data() {
        return {
            resultData: [
                { name: '图幅', value: 20 },
            ]

        }
    },
    mounted(){
    },
    methods: {
        chartClick(params){
            console.log(params);
        }
    }
}
<\/script>`

上述模版字符串自定义

多说一嘴: 如果要根据某个vue去拿模版字符串可通过raw-loader实现

解析

    data(){
        return {
            html: '',
            js: '',
            css: '',
        }
    }
    methods:{
         buildDom() {
            this.splitCode()
            if (this.html === '' || this.js === '') {
                return;
            }
            const common = new Function(this.js)()
            common.template = this.html
            //此时common就是vue模版对象了
            console.log(common);
            if (this.css !== '') {
                const styles = document.createElement('style')
                styles.type = 'text/css'
                styles.innerHTML = this.css
                document.getElementsByTagName('head')[0].appendChild(styles)
            }
        },
        splitCode() {
            const script = this.getSource(this.code, 'script').replace(
                /export default/,
                'return '
            )
            const style = this.getSource(this.code, 'style')
            const template =this.getSource(this.code, 'template')  
            this.js = script
            this.css = style
            this.html = template
        },
        getSource(source, type) {
            const regex = new RegExp(`<${type}[^>]*>`)
            let openingTag = source.match(regex)
            if (!openingTag) return ''
            else openingTag = openingTag[0]

            return source.slice(
                source.indexOf(openingTag) + openingTag.length,
                source.lastIndexOf(`</${type}>`)
            )
        },
        
    }
        
        

输出结果

相关推荐

  1. Vue原理解析】之模版编译

    2024-04-13 19:46:05       61 阅读
  2. vue管理系统模版

    2024-04-13 19:46:05       65 阅读
  3. Vue 模版编译原理

    2024-04-13 19:46:05       45 阅读
  4. Vue 模块化使用 Vuex

    2024-04-13 19:46:05       57 阅读
  5. vue 菜鸟教学如何jason字符串对象

    2024-04-13 19:46:05       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-13 19:46:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-13 19:46:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-13 19:46:05       82 阅读
  4. Python语言-面向对象

    2024-04-13 19:46:05       91 阅读

热门阅读

  1. 华为OD-C卷-分割均衡字符串[100分]

    2024-04-13 19:46:05       38 阅读
  2. 编程新手必看,Python3编程第一步语句学习(15)

    2024-04-13 19:46:05       42 阅读
  3. 一键升级 package.json 下所有依赖的版本

    2024-04-13 19:46:05       31 阅读
  4. Python学习入门(2)——进阶功能

    2024-04-13 19:46:05       32 阅读
  5. 计算机网络

    2024-04-13 19:46:05       32 阅读
  6. 美团 - 运维开发 - 春招复盘(更新中)

    2024-04-13 19:46:05       30 阅读
  7. 【NC235814】马踏棋盘

    2024-04-13 19:46:05       31 阅读
  8. Python将数据写入Mysql

    2024-04-13 19:46:05       46 阅读