翻译 Meteor React 制作 Todos - 02 - 组件
在 React 组件中定义视图在开始编写React视图库之前,要先添加 打开一个新的终端,在你运行 meteor add react 替换启动代码为了启动,我们来替换掉默认的启动应用的代码,接下来我们会讨论这是什么意思 首先,替换掉最开始的名为 <head> <title>Todo List</title> </head> <body> <div id="render-target"></div> </body> 然后,删除 新建 if (Meteor.isClient) { // 下面的代码最会在客户端运行 Meteor.startup(function () { // 在页面加载完成之后,使用 Meteor.startup 来渲染 React 组件 React.render(<App />,document.getElementById("render-target")); }); } 新建 // 不要在App前面添加 var App = React.createClass({ getTasks() { return [ { _id: 1,text: "This is task 1" },{ _id: 2,text: "This is task 2" },{ _id: 3,text: "This is task 3" } ]; },renderTasks() { return this.getTasks().map((task) => { return <Task key={task._id} task={task} />; }); },render() { return ( <div className="container"> <header> <h1>Todo List</h1> </header> <ul> {this.renderTasks()} </ul> </div> ); } }); 新建 // Task 组件 - 表示单个的待做事宜 Task = React.createClass({ propTypes: { // 这个组件从React的prop中接受task并显示 // 我们使用propTypes来表示这个属性是必须的 task: React.PropTypes.object.isRequired },render() { return ( <li>{this.props.task.text}</li> ); } }); 我们刚刚在应用中添加的三个事情分别是以下的意思:
在教程之后,我们会在添加和修改代码的时候参考这些组件 看看结果在我们的浏览器中,应用将会看起来像这个样子:
如果你的应用看起来并不是这样,请确认你的代码和例子上的一致 HTML文件中定义静态内容
每个在 <head>标签中的标签被添加到已被发送的HTML文件的 每一个在<template>中的标签将会被编译成 用React定义视图组件在React中,视图组件是被使用 从JSX的render方法中得到标记语言(markup)在React组件中最重要的方法就是 JSX文件可以使用ES2015的新特性如果你现在还没有尝试过下一代JavaScript特性,一些代码片段可能看起来有点怪异。这是因为
(arg) => {return result;}
render() {...}
你可以阅读ecmascript README来了解Meteor所支持的特性。想要更多关于 ECMAScript 2015 的知识可以看看下面的几个文章:
添加样式在我们做更多的未来打算之前,先来添加一些CSS来让我们的页面好看一点儿吧。 因为本教程专注于HTML和JavaScript,所以你可以拷贝下面的代码到 /* CSS 声明在这里 */ body { font-family: sans-serif; background-color: #315481; background-image: linear-gradient(to bottom,#315481,#918e82 100%); background-attachment: fixed; position: absolute; top: 0; bottom: 0; left: 0; right: 0; padding: 0; margin: 0; font-size: 14px; } .container { max-width: 600px; margin: 0 auto; min-height: 100%; background: white; } header { background: #d2edf4; background-image: linear-gradient(to bottom,#d0edf5,#e1e5f0 100%); padding: 20px 15px 15px 15px; position: relative; } #login-buttons { display: block; } h1 { font-size: 1.5em; margin: 0; margin-bottom: 10px; display: inline-block; margin-right: 1em; } form { margin-top: 10px; margin-bottom: -10px; position: relative; } .new-task input { box-sizing: border-box; padding: 10px 0; background: transparent; border: none; width: 100%; padding-right: 80px; font-size: 1em; } .new-task input:focus{ outline: 0; } ul { margin: 0; padding: 0; background: white; } .delete { float: right; font-weight: bold; background: none; font-size: 1em; border: none; position: relative; } li { position: relative; list-style: none; padding: 15px; border-bottom: #eee solid 1px; } li .text { margin-left: 10px; } li.checked { color: #888; } li.checked .text { text-decoration: line-through; } li.private { background: #eee; border-color: #ddd; } header .hide-completed { float: right; } .toggle-private { margin-left: 5px; } @media (max-width: 600px) { li { padding: 12px 15px; } .search { width: 150px; clear: both; } .new-task input { padding-bottom: 5px; } } 现在,你已经添加了这些CSS,这款应用应该看起来会更好一点了。切换到你的浏览器看看新的样式并确认其有没有加载成功吧~ 添加Sass(可选)
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |