我刚接触过反应。我想知道,像$rootScop,$q,$webSocket这样的棱角服务在reactJs中有等效的吗?
码
.service('a', function ($rootScope, $location, $q, $webSocket) {
this.init = function () {
b()
c()
}例如,以上的代码参数在反应上的等效性是什么?我知道react中等效的$scope是this.state。
发布于 2016-10-24 12:09:49
在反应中没有服务这种东西。
这里有其他的选择。
与服务类似的是,您可以编写一个类或函数,它将所有所需的服务作为params,您可以通过导出它在任何地方调用它。
一些类似的实现,您可以用于react。
在service.js中
const myService = (...otherServices) => {
doStuff1();
doStuff2();
return {
...items
}
}
export default myService;在component.js中
你可以进口它
import myService from './service';
import React from 'react';
class testComponent extends React.Component {
constructor(props){
super(props);
this.data = myService().getData(); //just an example.
}
render(){
return (
<div>{this.data}</div>
);
}
} 发布于 2016-10-24 11:58:45
$rootScope ->它是一个角度上的全局范围对象,在反应中我们使用减缩器来存储所有组件都可以访问的数据。
$q,->,我们有Q库和$q在反应中相同
$location ->在类/组件的实例中有转换/历史记录
$webScocket-> t这里有多个模块https://blog.pusher.com/making-reactjs-realtime-with-websockets/
https://stackoverflow.com/questions/40217475
复制相似问题