Created
April 5, 2017 16:36
-
-
Save beshanoe/54f256139a13639646bce0b59adb8247 to your computer and use it in GitHub Desktop.
How to use vue component in Angular 1.5 (based on react2angular)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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