using runtime html4,GitHub - alexjoverm/v-runtime-template: Vue component for compiling templates on...

v-runtime-template

68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f762d72756e74696d652d74656d706c6174652e737667

68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f646d2f762d72756e74696d652d74656d706c6174652e737667

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f6e6174652d70617970616c2d626c75652e737667

A Vue.js components that makes easy compiling and interpreting a Vue.js template at runtime by using a v-html like API.

Do you know 🦄

Motivation

This library solves the case where you get a vue-syntax template string on runtime, usually from a server. Think of a feature where you allow the user to create their own interfaces and structures. You save that as a vue template in your database, which your UI will request later. While components are pre-compiled at build time, this case isn't (since the template is received at runtime) and needs to be compiled at runtime.

v-runtime-template compiles that template and attaches it to the scope of the component that uses it, so it has access to its data, props, methods and computed properties.

Think of it as the v-html equivalent that also understands vue template syntax (while v-html is just for plain HTML).

Getting Started

Install it:

npm install v-runtime-template

You must use the with-compiler Vue.js version. This is needed in order to compile on-the-fly Vue.js templates. For that, you can set a webpack alias for vue to the vue/dist/vue.common file.

For example, if you use the Vue CLI, create or modify the vue.config.js file adding the following alias:

// vue.config.js

module.exports = {

runtimeCompiler: true

};

And in Nuxt, open the nuxt.config.js file and extend the webpack config by adding the following line to the extend key:

// nuxt.config.js

{

build: {

extend(config, { isDev, isClient }) {

config.resolve.alias["vue"] = "vue/dist/vue.common";

// ...

Usage

You just need to import the v-runtime-template component, and pass the template you want:

import VRuntimeTemplate from "v-runtime-template";

import AppMessage from "./AppMessage";

export default {

data: () => ({

name: "Mellow",

template: `

Hello {{ name }}!

`

}),

components: {

AppMessage,

VRuntimeTemplate

}

};

The template you pass have access to the parent component instance. For example, in the last example we're using the AppMessage component and accessing the {{ name }} state variable.

But you can access computed properties and methods as well from the template:

export default {

data: () => ({

name: "Mellow",

template: `

Hello {{ name }}!

Say Hi!

{{ someComputed }}

`,

}),

computed: {

someComputed() {

return "Wow, I'm computed";

},

},

methods: {

sayHi() {

console.log("Hi");

},

},

};

Limitations

Keep in mind that the template can only access the instance properties of the component who is using it. Read this issue for more information.

Comparison

v-runtime-template VS v-html

TL;DR: If you need to interpret only HTML, use v-html. Use this library otherwise.

They both have the same goal: to interpret and attach a piece of structure to a scope at runtime. The difference is, [v-html](https://vuejs.org/v2/api/#v-html) doesn't understand vue template syntax, but only HTML. So, while this code works:

export default {

data: () => ({

template: `

Go to Mike page

`

the following wouldn't since it uses the custom router-link component:

Go to Mike page

But you can use v-runtime-template, which uses basically the same API than v-html:

export default {

data: () => ({

template: `

Go to Mike page

`

v-runtime-template VS dynamic components ()

Dynamic components have somewhat different goal: to render a component dynamically by binding it to the is prop. Although, these components are usually pre-compiled. However, the goal of v-runtime-template can be achieved just by using the component options object form of dynamic components.

In fact, v-runtime-template uses that under the hood (in the render function form) along with other common tasks to achieve its goal.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值