资讯专栏INFORMATION COLUMN

[译] React.js 模式

mumumu / 776人阅读

摘要:此时,我将它写下来讨论和分享这些我发现的模式。正确的姿势应该是通过的方式获取子组件的一些信息。高阶组件是需要另外一个有用的模式依赖注入。也有部分人称它是一种模式。最直接的方式是通过每一层通过来传递。

原文出自:http://krasimirtsonev.com/blog/article/react-js-in-design-patterns

前言

我想找一个好的前端前端框架,找了很久。这个框架将能够帮助我写出具有可扩展性、可维护性 UI 的代码。通过对 React.js 优势的理解,我认为“我找到了它”。在我大量的使用过程中,我发现了一些模式性的东西。这些技术被一次又一次的用于编程开发之中。此时,我将它写下来、讨论和分享这些我发现的模式。

这些所有的代码都是可用的,能够在 https://github.com/krasimir/react-in-patterns 中下载。我可能不会更新我的博客,但是我将一直在 GitHub 中发布一些东西。我也将鼓励你在 GitHub 中讨论这些模式,通过 issue 或者直接 pull request 的方式。

一、React 自己的交流方式(Communication)

在使用 React 构建了几个月的情况下,你将能够体会到每一个 React Component 都是一个小系统,它能够自己运作。它有自己的 state、input、output.

Input

React Component 通过 props 作为 input(之后用输入代替)。下面我们来写一个例子:

// Title.jsx
class Title extends React.Component {
    render() {
        return 

{ this.props.text }

; } }; Title.propTypes = { text: React.PropTypes.string }; Title.defaultProps = { text: "Hello world" }; // App.jsx class App extends React.Component { render() { return ; } };</pre> <p>其中的 <b>Title</b> 组件只有一个输入 - <b>text</b>. 在父组件(App)提供了一个属性,通过 <b><Title></b> 组件。在 <b>Title</b> 组件中我们添加了两个设置 <b>propTypes</b> 和 <b>defaultProps</b>,我们来多带带看一下:</p> <p><p>propTypes - 定义 props 的类型,这将帮助我们告诉 React 我们将传什么类型的 prop,能够对这个 prop 进行验证(或者说是测试)。</p></p> <p><p>defaultProps - 定义 props 默认的值,设置一个默认值是一个好习惯。</p></p> <p>还有一个 <b>props.children</b> 属性,能够让我们访问到当前组件的子组件。比如:</p> <pre>class Title extends React.Component { render() { return ( <h1> { this.props.text } { this.props.children } </h1> ); } }; class App extends React.Component { render() { return ( <Title text="Hello React"> <span>community</span> ); } };

值得注意的是:如果我们没有在 Title 组件的 render 方法中添加 { this.props.children } 代码,其中的 span 标签(孩子组件)将不会被渲染。

对于一个组件的间接性输入(就是多层组件传递数据的时候),我们也可以调用 context 进行数据的访问。在整个 React tree 中的每一个组件中可能会有一个 context 对象。更多的说明将在依赖注入章节讲解。

Output

React 的输出就是渲染过后的 HTML 代码。在视觉上我们将看到一个 React 组件的样子。当然,有些组件可能包含一些逻辑,能够帮助我们传递一些数据或者触发一个事件行为(这类组件可能不会有具体的 UI 形态)。为了实现逻辑类型的组件,我们将继续使用组件的 props:

class Title extends React.Component {
    render() {
        return (
            

); } }; class App extends React.Component { render() { return ; } logoClicked() { console.log("logo clicked"); } };</pre> <p>我们通过一个 callback 的方式在子组件中进行调用,<b>logoClicked</b> 方法能够接受一些数据,这样我们就能够从子组件向父组件传输一些数据了(这里就是 React 方式的子组件向父组件通信)。</p> <p>我们之前有提到我们不能够访问 child 的 state。或者换句话说,我们不能够使用 this.props.children[0].state 的方式或者其他什么方式去访问。正确的姿势应该是通过 props callback 的方式获取子组件的一些信息。这是一件好事。这就迫使我们要去定义明确的 APIs,并鼓励使用单向数据流(在后面的<b>单向数据流</b>中将介绍)。</p> <b>二、组件构成(composition)</b> <pre><p>源码:https://github.com/krasimir/r...</p></pre> <p>另外一个很棒的是 React 的可组合性。对于我来说,除了 React 之外还没有发现有任何框架能够如此简单的方式去创建组件以及合并组件。这段我将探索一些组件的构建方式,来让开发工作更加棒。</p> <p>让我们先来看一个简单的例子:</p> <p><p>假设我们有一个应用,包含 header 部分,header 内部有一个 navigation(导航)组件。</p></p> <p><p>所以,我们将有三个 React 组件:App、Header 和 Navigation。</p></p> <p><p>他们是层级嵌套的关系。</p></p> <p>所以最后代码如下:</p> <pre><App> <Header> <Navigation> ... </Navigation> </Header> </App></pre> <p>我们为了组合这些小组件,并且引用他们,我们需要向下面这样定义他们:</p> <pre>// app.jsx import Header from "./Header.jsx"; export default class App extends React.Component { render() { return <Header />; } } // Header.jsx import Navigation from "./Navigation.jsx"; export default class Header extends React.Component { render() { return <header><Navigation /></header>; } } // Navigation.jsx export default class Navigation extends React.Component { render() { return (<nav> ... </nav>); } }</pre> <p>然而这样,我们用这种方式去组织组件会有几个问题:</p> <p><p>我们将 App 组件做为程序的入口,在这个组件里面去构建组件是一个不错的地方。对于 Header 组件,可能会包含其他组件,比如 logo、search 或者 slogan 之类的。它将是非常好处理,可以通过某种方式从外部传入,因此我们没有需要创建一个强依赖的组件。如果我们在另外的地方需要使用 Header 组件,但是这个时候又不需要内层的 Navigation 子组件。这个时候我们就不容易实现,因为 Header 和 Navigation 组件是两个强耦合的组件。</p></p> <p><p>这样编写组件是不容易测试的,我们可能在 Header 组件中有一些业务逻辑,为了测试 Header 组件,我们就必须要创建一个 Header 的实例(其实就是引用组件来渲染)。然而,又因为 Header 组件依赖了其他组件,这就导致了我们也可能需要创建一些其他组件的实例,这就让测试不是那么容易。并且我们在测试过程中,如果 Navigation 组件测试失败,也将导致 Header 组件测试失败,这将导致一个错误的测试结果(因为不会知道是哪个组件测试没有通过)。(注:然后在测试中 shallow rendering 解决了这个问题,能够只渲染 Header 组件,不用实例化其他组件)。</p></p> <p><strong>使用 React"s children API</strong></p> <p>在 React 中,我们能够通过 <b>this.props.children</b> 来很方便的处理这个问题。这个属性能够让父组件读取和访问子组件。这个 API 将使我们的 Header 组件更抽象和低耦合(原文是 dependency-free 不好翻译,但是是这个意思)。</p> <pre>// App.jsx export default class App extends React.Component { render() { return ( <Header> <Navigation /> </Header> ); } } // Header.jsx export default class Header extends React.Component { render() { return <header>{ this.props.children }</header>; } }</pre> <p>这将容易测试,因为我们可以让 Header 组件渲染成一个空的 div 标签。这就让组件脱离出来,然后只专注于应用的开发(其实就是抽象了一层父组件,然后让这个父组件和子组件进行了解耦,然后子组件可能才是应用的一些功能实现)。</p> <p><strong>将 child 做为一个属性</strong></p> <p>每一个 React 组件都接受 props。这非常好,这个 props 属性能包含一些数据。或者说是其他组件。</p> <pre>// App.jsx class App extends React.Component { render() { var title = <h1>Hello there!</h1>; return ( <Header title={ title }> <Navigation /> </Header> ); } }; // Header.jsx export default class Header extends React.Component { render() { return ( <header> { this.props.title } <hr /> { this.props.children } </header> ); } };</pre> <p>这个技术在我们要合并两个组件,这个组件在 Header 内部的时候是非常有用的,以及在外部提供这个需要合并的组件。</p> <b>三、高阶组件(Higher-order components)</b> <pre><p>源码:https://github.com/krasimir/r...</p></pre> <p>高阶组件看起来很像装饰器模式。他是包裹一个组件和附加一些其他功能或者 props 給它。</p> <p>这里通过一个函数来返回一个高阶组件:</p> <pre>var enhanceComponent = (Component) => class Enhance extends React.Component { render() { return ( <Component {...this.state} {...this.props} /> ) } }; export default enhanceComponent;</pre> <p>我们经常提供一个工厂函数,接受我们的原始组件,当我们需要访问的时候,就返回这个 被升级或者被包裹 过的组件版本給它。比如:</p> <pre>var OriginalComponent = () => <p>Hello world.</p>; class App extends React.Component { render() { return React.createElement(enhanceComponent(OriginalComponent)); } };</pre> <p>首先,高阶组件其实也是渲染的原始组件(传入的组件)。一个好的习惯是直接传入 state 和 props 給它。这将有助于我们想代理数据和像是用原始组件一样去使用这个高阶组件。</p> <p>高阶组件让我们能够控制输入。这些数据我们想通过 props 进行传递。现在像我们说的那样,我们有一个配置,OriginalComponent 组件需要这个配置的数据,代码如下:</p> <pre>var config = require("path/to/configuration"); var enhanceComponent = (Component) => class Enhance extends React.Component { render() { return ( <Component {...this.state} {...this.props} title={ config.appTitle } /> ) } };</pre> <p>这个配置是隐藏在高阶组件中。OriginalComponent 组件只能通过 props 来调用 title 数据。至于 title 数据从哪里来对于 OriginalComponent 来说并不重要(这就非常棒了!封闭性做的很好)。这是极大的优势,因为它帮助我们测试独立组件,以及提供一个好的机制去 mocking 数据。这里能够这样使用 title 属性( 也就是 stateless component[无状态组件] )。</p> <pre>var OriginalComponent = (props) => <p>{ props.title }</p>;</pre> <p>高阶组件是需要另外一个有用的模式-依赖注入(dependency injection)。</p> <b>四、依赖注入(Dependency injection)</b> <pre><p>源码:https://github.com/krasimir/r...</p></pre> <p>大部分模块/组件都会有依赖。能够合理的管理这些依赖能够直接影响到项目是否成功。有一个技术叫:依赖注入(dependency injection,之后我就简称 DI 吧)。也有部分人称它是一种模式。这种技术能够解决依赖的问题。</p> <p>在 React 中 DI 很容易实现,让我们跟着应用来思考:</p> <pre>// Title.jsx export default function Title(props) { return <h1>{ props.title }</h1>; } // Header.jsx import Title from "./Title.jsx"; export default function Header() { return ( <header> <Title /> </header> ); } // App.jsx import Header from "./Header.jsx"; class App extends React.Component { constructor(props) { super(props); this.state = { title: "React in patterns" }; } render() { return <Header />; } };</pre> <p>有一个 "React in patterns" 的字符串,这个字符串以某种方式来传递给 Title 组件。</p> <p>最直接的方式是通过: App => Header => Title 每一层通过 props 来传递。然而这样可能在这个三个组件的时候比较方便,但是如果有多个属性以及更深的组件嵌套的情况下将比较麻烦。大量组件将接收到它们并不需要的属性(因为是逐层传递)。</p> <p>我们前面提到的高阶组件的方式能够用来注入数据。让我们用这个技术来注入一下 title 变量。</p> <pre>// enhance.jsx var title = "React in patterns"; var enhanceComponent = (Component) => class Enhance extends React.Component { render() { return ( <Component {...this.state} {...this.props} title={ title } /> ) } }; // Header.jsx import enhance from "./enhance.jsx"; import Title from "./Title.jsx"; var EnhancedTitle = enhance(Title); export default function Header() { return ( <header> <EnhancedTitle /> </header> ); }</pre> <p>这个 title 是隐藏在中间层(高阶组件)中,我们通过 prop 来传递给 Title 组件。这很好的解决了,但是这只是解决了一半问题,现在我们没有层级的方式去传递 title,但是这些数据都在 echance.jsx 中间层组件。</p> <p>React 有一个 context 的概念,这个 context 能够在每一个组件中都可以访问它。这个优点像 event bus 模型,只不过这里是一个数据。这个方式让我们能够在任何地方访问到数据。</p> <pre>// 我们定义数据的地方:context => title var context = { title: "React in patterns" }; class App extends React.Component { getChildContext() { return context; } ... }; App.childContextTypes = { title: React.PropTypes.string }; // 我们需要这个数据的地方 class Inject extends React.Component { render() { var title = this.context.title; ... } } Inject.contextTypes = { title: React.PropTypes.string };</pre> <pre><p>值得注意的是我们必须使用 childContextTypes 和 contextTypes 这两个属性,定义这个上下文对象的类型声明。如果没有声明,context 这个对象将为空(经我测试,如果没有这些类型定义直接报错了,所以一定要记得加上哦)。这可能有些不太合适的地方,因为我们可能会放大量的东西在这里。所以说 context 定义成一个纯对象不是很好的方式,但是我们能够让它成为一个接口的方式来使用它,这将允许我们去存储和获取数据,比如:</p></pre> <pre>// dependencies.js export default { data: {}, get(key) { return this.data[key]; }, register(key, value) { this.data[key] = value; } }</pre> <p>然后,我们再看一下我们的例子,顶层的 App 组件可能就会像这样写:</p> <pre>import dependencies from "./dependencies"; dependencies.register("title", "React in patterns"); class App extends React.Component { getChildContext() { return dependencies; } render() { return <Header />; } }; App.childContextTypes = { data: React.PropTypes.object, get: React.PropTypes.func, register: React.PropTypes.func };</pre> <p>然后,我们的 Title 组件就从这个 context 中获取数据:</p> <pre>// Title.jsx export default class Title extends React.Component { render() { return <h1>{ this.context.get("title") }</h1> } } Title.contextTypes = { data: React.PropTypes.object, get: React.PropTypes.func, register: React.PropTypes.func };</pre> <p>最好的方式是我们在每次使用 context 的时候不想定义 contextTypes。这就是能够使用高阶组件包裹一层。甚至更多的是,我们能够写一个多带带的函数,去更好的描述和帮助我们声明这个额外的地方。之后通过 this.context.get("title") 的方式直接访问 context 数据。我们通过高阶组件获取我们需要的数据,然后通过 prop 的方式来传递给我们的原始组件,比如:</p> <pre>// Title.jsx import wire from "./wire"; function Title(props) { return <h1>{ props.title }</h1>; } export default wire(Title, ["title"], function resolve(title) { return { title }; });</pre> <p>这个 wire 函数有三个参数:</p> <p><p>一个 React 组件</p></p> <p><p>需要依赖的数据,这个数据以数组的方式定义</p></p> <p><p>一个 mapper 的函数,它能接受上下文的原始数据,然后返回一个我们的 React 组件(比如 Title 组件)实际需要的数据对象(相当于一个 filter 管道的作用)。</p></p> <p>这个例子我们只是通过这种方式传递来一个 title 字符串变量。然后在实际应用开发过程中,它可能是一个数据的存储集合,配置或者其他东西。因此,我们通过这种方式,我们能够通过哪些我们确实需要的数据,不用去污染组件,让它们接收一些并不需要的数据。</p> <p>这里的 wire 函数定义如下:</p> <pre>export default function wire(Component, dependencies, mapper) { class Inject extends React.Component { render() { var resolved = dependencies.map(this.context.get.bind(this.context)); var props = mapper(...resolved); return React.createElement(Component, props); } } Inject.contextTypes = { data: React.PropTypes.object, get: React.PropTypes.func, register: React.PropTypes.func }; return Inject; };</pre> <p>Inject 是一个高阶组件,它能够访问 context 对象的 dependencies 所有的配置项数组。这个 mapper 函数能够接收 context 的数据,并转换它,然后给 props 最后传递到我们的组件。</p> <p><strong>最后来看一下关于依赖注入</strong></p> <p>在很多解决方案中,都使用了依赖注入的技术,这些都基于 React 组件的 context 属性。我认为这很好的知道发生了什么。在写这篇文凭的时候,大量流行构建 React 应用的方式会需要 Redux。著名 connect 函数和 Provider 组件,就是使用的 context(现在大家可以看一下源码了)。</p> <p>我个人发现这个技术是真的有用。它是满足了我处理所有依赖数据的需要,使我的组件变得更加纯粹和更方便测试。</p> <b>五、单向数据流(One-way direction data flow)</b> <pre><p>源码:https://github.com/krasimir/r...</p></pre> <p>在 React 中单向数据流的模式运作的很好。它让组件不用修改数据,只是接收它们。它们只监听数据的改变和可能提供一些新的值,但是它们不会去改变数据存储器里面实际的数据。更新会放在另外地方的机制下,和组件只是提供渲染和新的值。</p> <p>让我们来看一个简单的 Switcher 组件的例子,这个组件包含了一个 button。我们点击它将能够控制切换(flag 不好翻译,程序员都懂的~)</p> <pre>class Switcher extends React.Component { constructor(props) { super(props); this.state = { flag: false }; this._onButtonClick = e => this.setState({ flag: !this.state.flag }); } render() { return ( <button onClick={ this._onButtonClick }> { this.state.flag ? "lights on" : "lights off" } </button> ); } }; // ... and we render it class App extends React.Component { render() { return <Switcher />; } };</pre> <p>这个时候再我们的组件里面有一个数据。或者换句话说:Switcher 只是一个一个我们需要通过 flag 变量来渲染的地方。让我们发送它到一个外面的 store 中:</p> <pre>var Store = { _flag: false, set: function (value) { this._flag = value; }, get: function () { return this._flag; } }; class Switcher extends React.Component { constructor(props) { super(props); this.state = { flag: false }; this._onButtonClick = e => { this.setState({ flag: !this.state.flag }, () => { this.props.onChange(this.state.flag); }); } } render() { return ( <button onClick={ this._onButtonClick }> { this.state.flag ? "lights on" : "lights off" } </button> ); } }; class App extends React.Component { render() { return <Switcher onChange={ Store.set.bind(Store) } />; } };</pre> <p>我们的 Store 对象是单例 我们有 helper 去设置和获取 _flag 这个属性的值。通过 getter,然后组件能够通过外部数据进行更新。大楷我们的应用工作流看起来是这样的:</p> <pre>User"s input | Switcher -------> Store </pre> <p>让我们假设我们要通过 Store 给后端服务去保存这个 flag 值。当用户返回的时候,我们必须设置合适的初始状态。如果用户离开后在后来,我们必须展示 "lights on" 而不是默认的 "lights off"。现在它变得困难,因为我们的数据是在两个地方。UI 和 Store 中都有自己的状态,我们必须在它们之间交流:Store --> Switcher 和 Switcher --> Store。</p> <pre>// ... in App component <Switcher value={ Store.get() } onChange={ Store.set.bind(Store) } /> // ... in Switcher component constructor(props) { super(props); this.state = { flag: this.props.value }; ... </pre> <p>我们的模型改变就要通过:</p> <pre>User"s input | Switcher <-------> Store ^ | | | | | | v Service communicating with our backend </pre> <p>所有这些都导致了需要管理两个状态而不是一个。如果 Store 的改变是通过其他系统的行为,我们就必须传送这些改变给 Switcher 组件和我们就增加了自己 App 的复杂度。</p> <p>单向数据流就解决了这个问题。它消除了这种多种状态的情况,只保留一个状态,这个状态一般是在 Store 里面。为了实现单向数据流这种方式,我们必须简单修改一下我们的 Store 对象。我们需要一个能够订阅改变的逻辑。</p> <pre>var Store = { _handlers: [], _flag: "", onChange: function (handler) { this._handlers.push(handler); }, set: function (value) { this._flag = value; this._handlers.forEach(handler => handler()) }, get: function () { return this._flag; } };</pre> <p>然后我们将有一个钩子在主要的 App 组件中,我们将在每次 Store 中的数据变化的时候重新渲染它。</p> <pre>class App extends React.Component { constructor(props) { super(props); Store.onChange(this.forceUpdate.bind(this)); } render() { return ( <div> <Switcher value={ Store.get() } onChange={ Store.set.bind(Store) } /> </div> ); } };</pre> <p><strong>注</strong>:我们使用了 forceUpdate 的方式,但这种方式不推荐使用。一般情况能够使用高阶组件进行重新渲染。我们使用 forceUpdate 只是简单的演示。</p> <p>因为这个改变,Switcher 变得比之前简单。我们不需要内部的 state:</p> <pre>class Switcher extends React.Component { constructor(props) { super(props); this._onButtonClick = e => { this.props.onChange(!this.props.value); } } render() { return ( <button onClick={ this._onButtonClick }> { this.props.value ? "lights on" : "lights off" } </button> ); } };</pre> <p>这个好处在于:这个模式让我们的组件变成了展示 Store 数据的一个填鸭式组件。它是真的让 React 组件变成了纯粹的渲染层。我们写我们的应用是声明的方式,并且只在一个地方处理一些复杂的数据。</p> <p>这个应用的工作流就变成了:</p> <pre>Service communicating with our backend ^ | v Store <----- | | v | Switcher ----> ^ | | User input </pre> <p>我们看到这个数据流都是一个方向流动的,并且在我们的系统中,不需要同步两个部分(或者更多部分)。单向数据流不止能基于 React 应用,这些就是它让应用变得更简单的原因,这个模式可能还需要更多的实践,但是它是确实值得探索的。</p> <b>六、结语</b> <p>当然,这不是在 React 中所有的设计模式/技术。还可能有更多的模式,你能够 checkout github.com/krasimir/react-in-patterns 进行更新。我将努力分享我新的发现。</p> </div> <div class="mt-64 tags-seach" > <div class="tags-info"> <a style="width:120px;" title="混合云" href="https://www.ucloud.cn/site/product/uhybrid.html">混合云</a> <a style="width:120px;" title="云服务器" href="https://www.ucloud.cn/site/product/uhost.html">云服务器</a> <a style="width:120px;" title="React.js" href="https://www.ucloud.cn/yun/tag/React.js/">React.js</a> <a style="width:120px;" title="react.js cdn" href="https://www.ucloud.cn/yun/tag/react.js cdn/">react.js cdn</a> <a style="width:120px;" title="linux下编译阿里" href="https://www.ucloud.cn/yun/tag/linuxxiabianyiali/">linux下编译阿里</a> <a style="width:120px;" title="linux下编译运行" href="https://www.ucloud.cn/yun/tag/linuxxiabianyiyunxing/">linux下编译运行</a> </div> </div> <div class="entry-copyright mb-30"> <p class="mb-15"> 文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。</p> <p>转载请注明本文地址:https://www.ucloud.cn/yun/80344.html</p> </div> <ul class="pre-next-page"> <li class="ellipsis"><a class="hpf" href="https://www.ucloud.cn/yun/80343.html">上一篇:关于easyui datebox 的选择器,属性 以及 取值。</a></li> <li class="ellipsis"><a class="hpf" href="https://www.ucloud.cn/yun/80345.html">下一篇:《JavaScript 闯关记》之对象</a></li> </ul> </div> <div class="about_topicone-mid"> <h3 class="top-com-title mb-0"><span data-id="0">相关文章</span></h3> <ul class="com_white-left-mid atricle-list-box"> <li> <div class="atricle-list-right"> <h2 class="ellipsis2"><a class="hpf" href="https://www.ucloud.cn/yun/85537.html"><b>Flux再进化:Introducing Relay and GraphQL<em>译</em></b></a></h2> <p class="ellipsis2 good">摘要:它的设计使得即使是大型团队也能以高度隔离的方式应对功能变更。获取数据数据变更性能,都是让人头痛的问题。通过维护组件与数据间的依赖在依赖的数据就绪前组件不会被渲染为开发者提供更加可预测的开发环境。这杜绝了隐式的数据依赖导致的潜在。 关于Relay与GraphQL的介绍 原文:Introducing Relay and GraphQL 视频地址(强烈建议观看):https://www.y...</p> <div class="com_white-left-info"> <div class="com_white-left-infol"> <a href="https://www.ucloud.cn/yun/u-1134.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/11/small_000001134.jpg" alt=""><span class="layui-hide64">cncoder</span></a> <time datetime="">2019-08-21 10:26</time> <span><i class="fa fa-commenting"></i>评论0</span> <span><i class="fa fa-star"></i>收藏0</span> </div> </div> </div> </li> <li> <div class="atricle-list-right"> <h2 class="ellipsis2"><a class="hpf" href="https://www.ucloud.cn/yun/111422.html"><b>即将立秋的《课多周刊》(第2期)</b></a></h2> <p class="ellipsis2 good">摘要:即将立秋的课多周刊第期我们的微信公众号,更多精彩内容皆在微信公众号,欢迎关注。若有帮助,请把课多周刊推荐给你的朋友,你的支持是我们最大的动力。课多周刊机器人运营中心是如何玩转起来的分享课多周刊是如何运营并坚持下来的。 即将立秋的《课多周刊》(第2期) 我们的微信公众号:fed-talk,更多精彩内容皆在微信公众号,欢迎关注。 若有帮助,请把 课多周刊 推荐给你的朋友,你的支持是我们最大...</p> <div class="com_white-left-info"> <div class="com_white-left-infol"> <a href="https://www.ucloud.cn/yun/u-1533.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/15/small_000001533.jpg" alt=""><span class="layui-hide64">ruicbAndroid</span></a> <time datetime="">2019-08-29 11:06</time> <span><i class="fa fa-commenting"></i>评论0</span> <span><i class="fa fa-star"></i>收藏0</span> </div> </div> </div> </li> <li> <div class="atricle-list-right"> <h2 class="ellipsis2"><a class="hpf" href="https://www.ucloud.cn/yun/80060.html"><b>即将立秋的《课多周刊》(第2期)</b></a></h2> <p class="ellipsis2 good">摘要:即将立秋的课多周刊第期我们的微信公众号,更多精彩内容皆在微信公众号,欢迎关注。若有帮助,请把课多周刊推荐给你的朋友,你的支持是我们最大的动力。课多周刊机器人运营中心是如何玩转起来的分享课多周刊是如何运营并坚持下来的。 即将立秋的《课多周刊》(第2期) 我们的微信公众号:fed-talk,更多精彩内容皆在微信公众号,欢迎关注。 若有帮助,请把 课多周刊 推荐给你的朋友,你的支持是我们最大...</p> <div class="com_white-left-info"> <div class="com_white-left-infol"> <a href="https://www.ucloud.cn/yun/u-1584.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/15/small_000001584.jpg" alt=""><span class="layui-hide64">MRZYD</span></a> <time datetime="">2019-08-20 10:14</time> <span><i class="fa fa-commenting"></i>评论0</span> <span><i class="fa fa-star"></i>收藏0</span> </div> </div> </div> </li> <li> <div class="atricle-list-right"> <h2 class="ellipsis2"><a class="hpf" href="https://www.ucloud.cn/yun/55534.html"><b>即将立秋的《课多周刊》(第2期)</b></a></h2> <p class="ellipsis2 good">摘要:即将立秋的课多周刊第期我们的微信公众号,更多精彩内容皆在微信公众号,欢迎关注。若有帮助,请把课多周刊推荐给你的朋友,你的支持是我们最大的动力。课多周刊机器人运营中心是如何玩转起来的分享课多周刊是如何运营并坚持下来的。 即将立秋的《课多周刊》(第2期) 我们的微信公众号:fed-talk,更多精彩内容皆在微信公众号,欢迎关注。 若有帮助,请把 课多周刊 推荐给你的朋友,你的支持是我们最大...</p> <div class="com_white-left-info"> <div class="com_white-left-infol"> <a href="https://www.ucloud.cn/yun/u-90.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/00/small_000000090.jpg" alt=""><span class="layui-hide64">K_B_Z</span></a> <time datetime="">2019-08-05 10:02</time> <span><i class="fa fa-commenting"></i>评论0</span> <span><i class="fa fa-star"></i>收藏0</span> </div> </div> </div> </li> <li> <div class="atricle-list-right"> <h2 class="ellipsis2"><a class="hpf" href="https://www.ucloud.cn/yun/79580.html"><b>[<em>译</em>] 前端攻略-从路人甲到英雄无敌二:JavaScript 与不断演化的框架</b></a></h2> <p class="ellipsis2 good">摘要:一般来说,声明式编程关注于发生了啥,而命令式则同时关注与咋发生的。声明式编程可以较好地解决这个问题,刚才提到的比较麻烦的元素选择这个动作可以交托给框架或者库区处理,这样就能让开发者专注于发生了啥,这里推荐一波与。 本文翻译自FreeCodeCamp的from-zero-to-front-end-hero-part。 继续译者的废话,这篇文章是前端攻略-从路人甲到英雄无敌的下半部分,在...</p> <div class="com_white-left-info"> <div class="com_white-left-infol"> <a href="https://www.ucloud.cn/yun/u-1389.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/13/small_000001389.jpg" alt=""><span class="layui-hide64">roadtogeek</span></a> <time datetime="">2019-08-19 18:25</time> <span><i class="fa fa-commenting"></i>评论0</span> <span><i class="fa fa-star"></i>收藏0</span> </div> </div> </div> </li> </ul> </div> <div class="topicone-box-wangeditor"> <h3 class="top-com-title mb-64"><span>发表评论</span></h3> <div class="xcp-publish-main flex_box_zd"> <div class="unlogin-pinglun-box"> <a href="javascript:login()" class="grad">登陆后可评论</a> </div> </div> </div> <div class="site-box-content"> <div class="site-content-title"> <h3 class="top-com-title mb-64"><span>0条评论</span></h3> </div> <div class="pages"></ul></div> </div> </div> <div class="layui-col-md4 layui-col-lg3 com_white-right site-wrap-right"> <div class=""> <div class="com_layuiright-box user-msgbox"> <a href="https://www.ucloud.cn/yun/u-687.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/06/small_000000687.jpg" alt=""></a> <h3><a href="https://www.ucloud.cn/yun/u-687.html" rel="nofollow">mumumu</a></h3> <h6>男<span>|</span>高级讲师</h6> <div class="flex_box_zd user-msgbox-atten"> <a href="javascript:attentto_user(687)" id="attenttouser_687" class="grad follow-btn notfollow attention">我要关注</a> <a href="javascript:login()" title="发私信" >我要私信</a> </div> <div class="user-msgbox-list flex_box_zd"> <h3 class="hpf">TA的文章</h3> <a href="https://www.ucloud.cn/yun/ut-687.html" class="box_hxjz">阅读更多</a> </div> <ul class="user-msgbox-ul"> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/124754.html">“创新实践”项目介绍1:《三维点云空间中的平面检测》</a></h3> <p>阅读 2136<span>·</span>2021-11-24 09:39</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/122316.html">软件测试就是简单的点点点吗???</a></h3> <p>阅读 1860<span>·</span>2021-10-12 10:12</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/120914.html">❤️程序人生 | 为什么越来越多的人用Python来做自动化测试?</a></h3> <p>阅读 623<span>·</span>2021-09-24 09:47</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/118309.html">CUBECLOUD:六周年庆钜惠,洛杉矶VPS套餐年付五折,全场85折优惠</a></h3> <p>阅读 1074<span>·</span>2021-08-19 11:12</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/112373.html">Typecho 主题制作记录</a></h3> <p>阅读 3330<span>·</span>2019-08-29 13:06</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/107440.html">原生js实现省市区三级联动插件</a></h3> <p>阅读 563<span>·</span>2019-08-26 11:43</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/104182.html">2分钟通过javascript的opener方式实现调用父窗口方法示例</a></h3> <p>阅读 2324<span>·</span>2019-08-23 17:20</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/103429.html">ReactElement源码解析</a></h3> <p>阅读 1038<span>·</span>2019-08-23 16:52</p></li> </ul> </div> <!-- 云社区相关服务 --> <div class="com_layuiright-box"> <h3 class="top-com-title"><span>云社区相关服务</span></h3> <div class="community-box flex_box flex_wrap community-box1"> <a href="https://www.ucloud.cn/yun/question/add.html" rel="nofollow"> <img src="https://www.ucloud.cn/yun/static/theme/ukd/images/topicone-icon1.png" alt="提问"> <span>提问</span> </a> <a href="https://www.ucloud.cn/yun/article" rel="nofollow"> <img src="https://www.ucloud.cn/yun/static/theme/ukd/images/topicone-icon2.png" alt="学习"> <span>学习</span> </a> <a href="https://www.ucloud.cn/yun/user/vertify.html" rel="nofollow"> <img src="https://www.ucloud.cn/yun/static/theme/ukd/images/topicone-icon4.png" alt="认证"> <span>认证</span> </a> <a href="https://www.ucloud.cn/site/product/uhost.html?ytag=seo" rel="nofollow"> <img src="https://www.ucloud.cn/yun/static/theme/ukd/images/topicone-icon5.png" alt="产品"> <span>产品</span> </a> <a href="https://spt.ucloud.cn/30001?ytag=seo" rel="nofollow"> <img src="https://www.ucloud.cn/yun/static/theme/ukd/images/topicone-icon6.png" alt="技术服务"> <span>技术服务</span> </a> <a href="https://spt.ucloud.cn/30001?ytag=seo" rel="nofollow"> <img src="https://www.ucloud.cn/yun/static/theme/ukd/images/topicone-icon3.png" alt="售前咨询"> <span>售前咨询</span> </a> </div> </div> <!-- 文章详情右侧广告--> <div class="com_layuiright-box"> <h6 class="top-com-title"><span>最新活动</span></h6> <div class="com_adbox"> <div class="layui-carousel" id="right-item"> <div carousel-item> <div> <a href="https://www.ucloud.cn/site/product/uhost.html" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/220620/Z7TLrpAi.png" alt="云服务器"> </a> </div> <div> <a href="https://www.ucloud.cn/site/product/uhybrid.html" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/220620/MWraMsBh.png" alt="混合云"> </a> </div> <div> <a href="https://www.ucloud.cn/site/product/ucloudstack.html" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/220620/ifzOxvjW.png" alt="私有云"> </a> </div> <div> <a href="https://www.ucloud.cn/site/product/utrion.html" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/220620/VDqwC1iN.png" alt="超融合服务器"> </a> </div> <div> <a href="https://www.ucloud.cn/site/product/uhybrid.html" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/220630/pJwnviKN.png" alt="服务器托管"> </a> </div> <div> <a href="https://www.ucloud.cn/site/product/uxzone.html" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/220630/CDb5uXxp.jpeg" alt="idc机房托管"> </a> </div> <div> <a href="https://www.ucloud.cn/site/active/network.html?ytag=seo" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/230227/XWsSXmvm.png" alt="专线服务"> </a> </div> </div> </div> </div> <!-- banner结束 --> <div class="adhtml"> </div> <script> $(function(){ $.ajax({ type: "GET", url:"https://www.ucloud.cn/yun/ad/getad/1.html", cache: false, success: function(text){ $(".adhtml").html(text); } }); }) </script> </div> </div> </div> </div> </div> </section> <!-- wap拉出按钮 --> <div class="site-tree-mobile layui-hide"> <i class="layui-icon layui-icon-spread-left"></i> </div> <!-- wap遮罩层 --> <div class="site-mobile-shade"></div> <!--付费阅读 --> <div id="payread"> <div class="layui-form-item">阅读需要支付1元查看</div> <div class="layui-form-item"><button class="btn-right">支付并查看</button></div> </div> <script> var prei=0; $(".site-seo-depict pre").each(function(){ var html=$(this).html().replace("<code>","").replace("</code>","").replace('<code class="javascript hljs" codemark="1">',''); $(this).attr('data-clipboard-text',html).attr("id","pre"+prei); $(this).html("").append("<code>"+html+"</code>"); prei++; }) $(".site-seo-depict img").each(function(){ if($(this).attr("src").indexOf('data:image/svg+xml')!= -1){ $(this).remove(); } }) $("LINK[href*='style-49037e4d27.css']").remove(); $("LINK[href*='markdown_views-d7a94ec6ab.css']").remove(); layui.use(['jquery', 'layer','code'], function(){ $("pre").attr("class","layui-code"); $("pre").attr("lay-title",""); $("pre").attr("lay-skin",""); layui.code(); $(".layui-code-h3 a").attr("class","copycode").html("复制代码 ").attr("onclick","copycode(this)"); }); function copycode(target){ var id=$(target).parent().parent().attr("id"); var clipboard = new ClipboardJS("#"+id); clipboard.on('success', function(e) { e.clearSelection(); alert("复制成功") }); clipboard.on('error', function(e) { alert("复制失败") }); } //$(".site-seo-depict").html($(".site-seo-depict").html().slice(0, -5)); </script> <link rel="stylesheet" type="text/css" href="https://www.ucloud.cn/yun/static/js/neweditor/code/styles/tomorrow-night-eighties.css"> <script src="https://www.ucloud.cn/yun/static/js/neweditor/code/highlight.pack.js" type="text/javascript"></script> <script src="https://www.ucloud.cn/yun/static/js/clipboard.js"></script> <script>hljs.initHighlightingOnLoad();</script> <script> function setcode(){ var _html=''; document.querySelectorAll('pre code').forEach((block) => { var _tmptext=$.trim($(block).text()); if(_tmptext!=''){ _html=_html+_tmptext; console.log(_html); } }); } </script> <script> function payread(){ layer.open({ type: 1, title:"付费阅读", shadeClose: true, content: $('#payread') }); } // 举报 function jupao_tip(){ layer.open({ type: 1, title:false, shadeClose: true, content: $('#jubao') }); } $(".getcommentlist").click(function(){ var _id=$(this).attr("dataid"); var _tid=$(this).attr("datatid"); $("#articlecommentlist"+_id).toggleClass("hide"); var flag=$("#articlecommentlist"+_id).attr("dataflag"); if(flag==1){ flag=0; }else{ flag=1; //加载评论 loadarticlecommentlist(_id,_tid); } $("#articlecommentlist"+_id).attr("dataflag",flag); }) $(".add-comment-btn").click(function(){ var _id=$(this).attr("dataid"); $(".formcomment"+_id).toggleClass("hide"); }) $(".btn-sendartcomment").click(function(){ var _aid=$(this).attr("dataid"); var _tid=$(this).attr("datatid"); var _content=$.trim($(".commenttext"+_aid).val()); if(_content==''){ alert("评论内容不能为空"); return false; } var touid=$("#btnsendcomment"+_aid).attr("touid"); if(touid==null){ touid=0; } addarticlecomment(_tid,_aid,_content,touid); }) $(".button_agree").click(function(){ var supportobj = $(this); var tid = $(this).attr("id"); $.ajax({ type: "GET", url:"https://www.ucloud.cn/yun/index.php?topic/ajaxhassupport/" + tid, cache: false, success: function(hassupport){ if (hassupport != '1'){ $.ajax({ type: "GET", cache:false, url: "https://www.ucloud.cn/yun/index.php?topic/ajaxaddsupport/" + tid, success: function(comments) { supportobj.find("span").html(comments+"人赞"); } }); }else{ alert("您已经赞过"); } } }); }); function attenquestion(_tid,_rs){ $.ajax({ //提交数据的类型 POST GET type:"POST", //提交的网址 url:"https://www.ucloud.cn/yun/favorite/topicadd.html", //提交的数据 data:{tid:_tid,rs:_rs}, //返回数据的格式 datatype: "json",//"xml", "html", "script", "json", "jsonp", "text". //在请求之前调用的函数 beforeSend:function(){}, //成功返回之后调用的函数 success:function(data){ var data=eval("("+data+")"); console.log(data) if(data.code==2000){ layer.msg(data.msg,function(){ if(data.rs==1){ //取消收藏 $(".layui-layer-tips").attr("data-tips","收藏文章"); $(".layui-layer-tips").html('<i class="fa fa-heart-o"></i>'); } if(data.rs==0){ //收藏成功 $(".layui-layer-tips").attr("data-tips","已收藏文章"); $(".layui-layer-tips").html('<i class="fa fa-heart"></i>') } }) }else{ layer.msg(data.msg) } } , //调用执行后调用的函数 complete: function(XMLHttpRequest, textStatus){ postadopt=true; }, //调用出错执行的函数 error: function(){ //请求出错处理 postadopt=false; } }); } </script> <footer> <div class="layui-container"> <div class="flex_box_zd"> <div class="left-footer"> <h6><a href="https://www.ucloud.cn/"><img src="https://www.ucloud.cn/yun/static/theme/ukd//images/logo.png" alt="UCloud (优刻得科技股份有限公司)"></a></h6> <p>UCloud (优刻得科技股份有限公司)是中立、安全的云计算服务平台,坚持中立,不涉足客户业务领域。公司自主研发IaaS、PaaS、大数据流通平台、AI服务平台等一系列云计算产品,并深入了解互联网、传统企业在不同场景下的业务需求,提供公有云、混合云、私有云、专有云在内的综合性行业解决方案。</p> </div> <div class="right-footer layui-hidemd"> <ul class="flex_box_zd"> <li> <h6>UCloud与云服务</h6> <p><a href="https://www.ucloud.cn/site/about/intro/">公司介绍</a></p> <p><a href="https://zhaopin.ucloud.cn/" >加入我们</a></p> <p><a href="https://www.ucloud.cn/site/ucan/onlineclass/">UCan线上公开课</a></p> <p><a href="https://www.ucloud.cn/site/solutions.html" >行业解决方案</a></p> <p><a href="https://www.ucloud.cn/site/pro-notice/">产品动态</a></p> </li> <li> <h6>友情链接</h6> <p><a href="https://www.surfercloud.com/">SurferCloud</a></p> <p><a href="https://ucloudstack.com/" >私有云</a></p><p><a href="https://pinex.it" >pinex</a></p> <p><a href="https://www.renyucloud.com/" ></a></p> <p><a href="https://www.picpik.ai" >AI Art Generator</a></p> <p><a href="https://www.uwin-link.com" >工厂仿真软件</a></p> </li> <li> <h6>社区栏目</h6> <p><a href="https://www.ucloud.cn/yun/column/index.html">专栏文章</a></p> <p><a href="https://www.ucloud.cn/yun/ask/">专业问答</a></p> <p><a href="https://www.ucloud.cn/yun/kc.html">云学院</a></p> <p><a href="https://www.ucloud.cn/yun/udata/">专题地图</a></p> </li> <li> <h6>常见问题</h6> <p><a href="https://www.ucloud.cn/site/ucsafe/notice.html" >安全中心</a></p> <p><a href="https://www.ucloud.cn/site/about/news/recent/" >新闻动态</a></p> <p><a href="https://www.ucloud.cn/site/about/news/report/">媒体动态</a></p> <p><a href="https://www.ucloud.cn/site/cases.html">客户案例</a></p> <p><a href="https://www.ucloud.cn/site/notice/">公告</a></p> </li> <li> <span><img src="https://static.ucloud.cn/7a4b6983f4b94bcb97380adc5d073865.png" alt="优刻得"></span> <p>扫扫了解更多</p></div> </div> <div class="copyright">Copyright © 2012-2023 UCloud 优刻得科技股份有限公司<i>|</i><a rel="nofollow" href="http://beian.miit.gov.cn/">沪公网安备 31011002000058号</a><i>|</i><a rel="nofollow" href="http://beian.miit.gov.cn/"></a> 沪ICP备12020087号-3</a><i>|</i> <script type="text/javascript" src="https://gyfk12.kuaishang.cn/bs/ks.j?cI=197688&fI=125915" charset="utf-8"></script> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?290c2650b305fc9fff0dbdcafe48b59d"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-DZSMXQ3P9N"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-DZSMXQ3P9N'); </script> <script> (function(){ var el = document.createElement("script"); el.src = "https://lf1-cdn-tos.bytegoofy.com/goofy/ttzz/push.js?99f50ea166557aed914eb4a66a7a70a4709cbb98a54ecb576877d99556fb4bfc3d72cd14f8a76432df3935ab77ec54f830517b3cb210f7fd334f50ccb772134a"; el.id = "ttzz"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(el, s); })(window) </script></div> </div> </footer> </body> <script src="https://www.ucloud.cn/yun/static/theme/ukd/js/common.js"></script> <<script type="text/javascript"> $(".site-seo-depict *,.site-content-answer-body *,.site-body-depict *").css("max-width","100%"); </script> </html>