-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSX #44
Comments
React JSX |
Vue JSXJSX本质上是
参考: SyntaxVia. https://github.com/vuejs/jsx Contentrender() {
return <p>hello</p>
} with dynamic content: render() {
return <p>hello { this.message }</p>
} when self-closing: render() {
return <input />
} with a component: import MyComponent from './my-component'
export default {
render() {
return <MyComponent>hello</MyComponent>
},
}
Attributes/Propsrender() {
return <input type="email" />
} with a dynamic binding: render() {
return <input
type="email"
placeholder={this.placeholderText}
/>
} with the spread operator (object needs to be compatible with Vue Data Object): render() {
const inputAttrs = {
type: 'email',
placeholder: 'Enter your email'
}
return <input {...{ attrs: inputAttrs }} />
} Slots
// slots
render() {
return (<div>{this.$slots.default}</div>)
}
// scoped slots
render() {
return (<div>{this.$slots.default({ name: 'John' })}</div>)
}
render() {
return (
<MyComponent>
<header slot="header">header</header>
<footer slot="footer">footer</footer>
</MyComponent>
)
}
render() {
const scopedSlots = {
header: () => <header>header</header>,
footer: () => <footer>footer</footer>
}
return <MyComponent scopedSlots={scopedSlots} />
} Directivesv-model<input vModel={this.newTodoText} /> with a modifier: <input vModel_trim={this.newTodoText} />
// or
<el-input
value={this.inputValue}
on-input={val => this.inputValue = val.trim()}
/> v-text 和 v-html// domPropsInnerText 代替 v-text
<div domPropsInnerText={this.content}></div>
// domPropsInnerHTML 代替 v-html
<p domPropsInnerHTML={html} />
v-if 和 v-for模板中使用的 Event & Key Modifiers
<input vOn:click={this.newTodoText} />
<input vOn:click_stop_prevent={this.newTodoText} />
const data = {
on: { click: this.clickHandler },
nativeOn: { mouseenter: this.mouseenterHandler }
}
render() {
return (
<MyComponent { ...data }></MyComponent>
)
} 事件修饰符:
高阶组件中的 const data = {
attrs: this.$attrs,
on: {
...this.$listeners,
click() {},
}
}
<button { ...data }><button> 自定义指令自定义指令在 在模板里: <template>
<div v-loading.fullscreen.lock = "loading">...</div>
</template>
render() {
/**
* modifiers指定修饰符,如果使用某一个修饰符,则指定这个修饰符的值为 true
* 不使用可以设置为false或者直接删掉
*/
const directives = [
{
name: 'loading',
value: this.loading,
modifiers: { fullscreen: true, lock: false }
}
]
return (
<div {
...{ directives }
}>加载内容</div>
)
} Functional ComponentsTranspiles arrow functions that return JSX into functional components, when they are either default exports: export default ({ props }) => <p>hello {props.message}</p> or PascalCase variable declarations: const HelloWorld = ({ props }) => <p>hello {props.message}</p> Vue Data ObjectVia. https://cn.vuejs.org/v2/guide/render-function.html {
// 与 `v-bind:class` 的 API 相同,
// 接受一个字符串、对象或字符串和对象组成的数组
'class': {
foo: true,
bar: false
},
// 与 `v-bind:style` 的 API 相同,
// 接受一个字符串、对象,或对象组成的数组
style: {
color: 'red',
fontSize: '14px'
},
// 普通的 HTML attribute
attrs: {
id: 'foo'
},
// 组件 prop
props: {
myProp: 'bar'
},
// DOM property
domProps: {
innerHTML: 'baz'
},
// 事件监听器在 `on` 内,
// 但不再支持如 `v-on:keyup.enter` 这样的修饰器。
// 需要在处理函数中手动检查 keyCode。
on: {
click: this.clickHandler
},
// 仅用于组件,用于监听原生事件,而不是组件内部使用
// `vm.$emit` 触发的事件。
nativeOn: {
click: this.nativeClickHandler
},
// 自定义指令。注意,你无法对 `binding` 中的 `oldValue`
// 赋值,因为 Vue 已经自动为你进行了同步。
directives: [
{
name: 'my-custom-directive',
value: '2',
expression: '1 + 1',
arg: 'foo',
modifiers: {
bar: true
}
}
],
// 作用域插槽的格式为
// { name: props => VNode | Array<VNode> }
scopedSlots: {
default: props => createElement('span', props.text)
},
// 如果组件是其它组件的子组件,需为插槽指定名称
slot: 'name-of-slot',
// 其它特殊顶层 property
key: 'myKey',
ref: 'myRef',
// 如果你在渲染函数中给多个元素都应用了相同的 ref 名,
// 那么 `$refs.myRef` 会变成一个数组。
refInFor: true
} Functional ComponentsVia. https://cn.vuejs.org/v2/guide/render-function.html
Vue.component('my-component', {
functional: true,
// Props 是可选的
props: {
// ...
},
// 为了弥补缺少的实例
// 提供第二个参数作为上下文
render: function (createElement, context) {
// ...
}
}) 组件需要的一切都是通过
VNode
|
No description provided.
The text was updated successfully, but these errors were encountered: