能夠使用jsx 方便組件的開發app
主要是render 函數dom
class MyComponent { render() { return ( <div> <h1>Hello World</h1> <p>This is JSX!</p> </div> ); } }
render() { return ( <div>Hello {this.name}</div> ) }
render() { if (this.name) { return ( <div>Hello {this.name}</div> ) } else { return ( <div>Hello, World</div> ) } }
參考函數
render() { return ( <div> <h2>A Component</h2> <div><slot /></div> </div> ); }
對於多個能夠指定名稱
參考oop
render(){ return [ <slot name="item-start" />, <h1>Here is my main content</h1>, <slot name="item-end" /> ] } render(){ return( <my-component> <p slot="item-start">I'll be placed before the h1</p> <p slot="item-end">I'll be placed after the h1</p> </my-component> ) }
參考this
render() { return ( <div> {this.todos.map((todo) => <div> <div>{todo.taskName}</div> <div>{todo.isCompleted}</div> </div> )} </div> ) }
使用原生dom 事件
參考code
export class MyComponent { handleClick(event: UIEvent) { alert('Received the button click!'); } render() { return ( <button onClick={ (event: UIEvent) => this.handleClick(event)}>Click Me!</button> ); } }
使用ref 進行參數綁定
參考component
@Component({ tag: 'app-home', }) export class AppHome{ textInput: HTMLInputElement; handleSubmit = (ev: Event) => { ev.preventDefault(); console.log(this.textInput.value); } render() { return ( <form onSubmit={this.handleSubmit}> <label> Name: <input type="text" ref={(el: HTMLInputElement) => this.textInput = el} /> </label> <input type="submit" value="Submit" /> </form> ); } }