Skip to content

Instantly share code, notes, and snippets.

@beshanoe
Created April 5, 2017 16:36
Show Gist options
  • Save beshanoe/54f256139a13639646bce0b59adb8247 to your computer and use it in GitHub Desktop.
Save beshanoe/54f256139a13639646bce0b59adb8247 to your computer and use it in GitHub Desktop.
How to use vue component in Angular 1.5 (based on react2angular)
import { IAugmentedJQuery, IComponentOptions } from 'angular'
import { fromPairs, keys, merge } from 'lodash'
import NgComponent from 'ngcomponent'
import * as Vue from 'vue'
export function vue2angular<Props>(
Component: Vue.ComponentOptions<Vue>
): IComponentOptions {
let names: string[];
if (Component.props) {
if (Array.isArray(Component.props)) {
names = Component.props;
} else if (typeof Component.props === 'object') {
names = keys(Component.props);
}
} else {
names = [];
}
return {
bindings: fromPairs(names.map(_ => [_, '<'])),
controller: ['$element', class extends NgComponent<Props, void> {
private vueInstance;
private observedProps = fromPairs(names.map(_ => [_, '']));
constructor(private $element: IAugmentedJQuery) {
super();
}
render() {
merge(this.observedProps, this.props);
if (!this.vueInstance) {
this.vueInstance = new Vue({
el: this.$element[0],
data: this.observedProps,
render: h => h(Component, {
attrs: {
...this.observedProps
}
})
})
}
}
}]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment