How can I get the component code before compile in Vue2.x? - vue.js

What problem does this feature solve?
<code-box title="基本" describe="button基本用法">
<i-button>Default</i-button>
</code-box>
I want to get default Slot String like <i-button>Default</i-button> in codeBox.
I can find then api _slotContents in vue 1.x.
Is there any way can get the same function in vue 2.x?
What does the proposed API look like?
_slotContents in vue 1.x

If I understand correctly, you want to use scoped slots.
If for some reason you really want to get the rendered html inside your component you can use this.$el.innerHtml.

Related

Is it possible to have dynamic element names in a Vue template?

I need to have a component, where I get the type of another component that this component should create. (it could be one of n different elements) Doing this in the component's render function is not a problem, but since I am new to Vue and I am trying to do things the Vue way, not the way I would do it in React.
I have not been able to find any solution with just using a Vue template. Is there one?
This is how I would like to be able to use the component:
<factory elm="first-component"></factory>
The factory should then internally in some way result in this:
<first-component></first-component>
(it should also be able to add attributes to the component, but that I know how to do, so a suggested solution does not need to care about attributes)
There is <component :is="myCoolComponent"></component> that will basically generate <my-cool-component></my-cool-component>.
Is it what you're looking for?
From the documentation: https://v2.vuejs.org/v2/guide/components-dynamic-async.html#keep-alive-with-Dynamic-Components
You could also totally create a Factory.vue component and put that kind of logic inside of it (<component :is="" ...>).

Is it possible to use more than one "template" element in one component in vuejs?

This question triggered in my mind while reading through Vue's official guide here.
I don't know if I find the answer to this question on proceeding further to read more from the official guide, but curious to know if it's possible or not.
Well, I'll update here as soon as I got to know the answer.
Screenshot from official guide
A component must use a single template or component option object, and that template must provide a single root element to mount. The template is converted into a render function internally, and Vue can only have 1 render function.
Your component can in turn contain a component that is dynamic, however.
https://v2.vuejs.org/v2/guide/components-dynamic-async.html

Vue.JS check if vue-i18n is installed

i got a little Problem, i'm creating a vuejs package in which i use vue-i18n to translate things.
The Problem is, if the user haven't got vue-i18n installed it breaks the package (obviously since i use it).
Do you know a way to prevent this?
My first approach was to just v-if the translations in the template to check if i18n is registered within window, but that obviously won't work if a user names the object differently.
you can check if this.$i18n exists

How to do two way binding in custom directives in Vue.js 2?

Hi I am looking for a solution for two way binding in Vue.js 2. I found a answer here about two way binding but it is in Vue.js 1. I tried to work it in Vue.js 1. But it didn't work. Can anyone help me here.
Why I want that?
A - I have a contenteditable div and a span tag inside which is shows a variable with v-html. But I want some functions to call when some edit happens. #change and #keyup don't work with span in vuejs 1. So I moved to custom directives.
To support something like this in vue2.0: as suggested by Evan You in forum here is to put these kind of functionality in a component and to reuse the same functionality on multiple components you can extract it into a shared mixin.
My suggestion is that the logic should be handled inside the component. In 2.0 v-model can work on components, e.g. https://github.com/vuejs/vue/blob/next/examples/select2/index.html
If you want to reuse the same logic on multiple components you can extract it into a shared mixin.

Compile string with custom elements

I have an Aurelia application in which I'm trying to build a CMS component. This component will load data from the server and this data mainly contains slug, title and content fields.
I also have several global components defined in my application, and I want to be able to use those components in the server so when I pull that data my CMS component is able to transform/compile those custom elements.
An example would be a tab component. I have the tab component with this structure defined:
<tab-panel>
<tab title="First"></tab>
<tab title="Second"></tab>
</tab-panel>
The CMS component will contain a content property which I use to pass a string like this: '<tab-panel><tab title="First"></tab><tab title="Second"></tab></tab-panel>'
The component needs to compile that string and render it in its view. I've checked the enhance API, but it doesn't worked, at least for me. Any other suggestion to dynamically compile/render custom elements??
Thanks a lot in advance.
I've found the solution. I've used a compose element and InlineViewStrategy and it worked well, the components are shows and binding works as expected.
If your custom elements are registered globally using globalResources you can actually using the TemplatingEngine to dynamically insert content into the DOM and then compile it after-the-fact. This blog post goes into detail in how you can do it.
However, I would use this as a last resort. As is mostly always the case, there are much better ways to do something in Aurelia. Using the <compose> element is a great way to dynamically render content in your Aurelia applications and should always be the first port of call.