我有一个带有 URL 参数的 React 组件。当我运行测试并安装组件时,参数始终未定义,因此会中断测试。我试图将它们硬编码为常量、道具,但仍然无法正常工作。我可以尝试其他任何想法吗?
import React, { Component } from 'react';
class BarcodePage extends Component {
constructor(props) {
super(props);
}
componentDidMount() {
const { SKU, ID } = this.props.match.params
}
render() {
return (
<h1>Barcode view {SKU} {ID}</h1>
);
}
}
export default BarcodePage;
import React from 'react';
import { shallow } from 'enzyme';
import BarcodePage from './BarcodePage';
const component = mount(
<BarcodePage params={{SKU: '1111', ID: '2121212' }} />
);
describe('<BarcodePage />', () => {
it('render one header', () …Run Code Online (Sandbox Code Playgroud) 我不确定我在这里缺少什么,但我正在尝试更新数组中的值,然后将其反映在转发器中.在应用更新之前,我是否需要制作副本或其他内容才能让Angular显示更改?
---样品json
"lists": [
{
"name": "one",
"enabled": true
},
{
"name": "two",
"enabled": false
}
]
Run Code Online (Sandbox Code Playgroud)
! - 代码
setTimeout(function(){
$scope.$apply(function(){
$scope.lists[1].enabled = true;
});
},1);
Run Code Online (Sandbox Code Playgroud)
!--- html
<span data-ng-repeat="list in lists | filter: {enabled: true}"></span>
Run Code Online (Sandbox Code Playgroud) 我正在遵循ui-router wiki上与使用resolve相关的说明.如果我按照维基上的说明操作,我会在没有问题的情况下获得共享给控制器的值,如果我通过引用包含控制器,则值是未定义的.我错过了什么?
.state('intro', {
url: '/',
resolve:{
resA: function(){
return {'value': 'A'};
}
},
views: {
'content': {
templateUrl: 'views/home.html',
controller: function($scope, resA){
$scope.resA = resA.value;
}
},
'navigation': {
templateUrl: 'views/navigation.html'
}
} })
Run Code Online (Sandbox Code Playgroud)
.controller('introController', ['$scope', function($scope,resA) {
$scope.resA = resA.value; //undefined
Run Code Online (Sandbox Code Playgroud) 我正在尝试测试包含服务的基本角度2组件.当我运行测试时,我遇到以下错误.组件,服务和测试都很小,它们在浏览器中运行良好.我不确定我在测试中缺少什么才能使它工作.
任何帮助表示赞赏.
Uncaught Error: Cannot set base providers because it has already been called(…)
Uncaught Error: Error in ./TestComponent class TestComponent - inline template:0:0 caused by: No provider for Http!
Error: No provider for Http!
at NoProviderError.BaseError [as constructor] (http://localhost:9876/base/karma-shim.js:4006:34)
at NoProviderError.AbstractProviderError [as constructor] (http://localhost:9876/base/karma-shim.js:33221:16)
at new NoProviderError (http://localhost:9876/base/karma-shim.js:33252:16)
at ReflectiveInjector_._throwOrNull (http://localhost:9876/base/karma-shim.js:57969:19)
at ReflectiveInjector_._getByKeyDefault (http://localhost:9876/base/karma-shim.js:57997:25)
at ReflectiveInjector_._getByKey (http://localhost:9876/base/karma-shim.js:57960:25)
at ReflectiveInjector_.get (http://localhost:9876/base/karma-shim.js:57769:21)
at TestBed.get (http://localhost:9876/base/karma-shim.js:5778:67)
at ElementInjector.get (http://localhost:9876/base/karma-shim.js:58133:48)
at _View_TestComponent0.createInternal (TestComponent.ngfactory.js:21:68)
at _View_TestComponent0.AppView.create (http://localhost:9876/base/karma-shim.js:58492:21)
at _View_TestComponent0.DebugAppView.create (http://localhost:9876/base/karma-shim.js:58704:44)
at _View_TestComponent_Host0.createInternal (TestComponent_Host.ngfactory.js:18:14)
at …Run Code Online (Sandbox Code Playgroud) 我正在使用带有React的typescript 2.3.4.我收到错误TS7006:参数'props'隐含有'any'类型.什么是在打字稿中投放道具的正确方法?
任何帮助表示赞赏.
interface State {
name: string;
}
interface Props {
}
export class Profile extends React.Component<Props, State> {
public state: State;
public props: Props;
constructor(props){
super(props);
this.state = {
name: 'baz111'
};
}
public render() {
return (
<section>
<section>
<h3>profile 1</h3>
<div>baz</div>
</section>
<section>
<h3>profile 2</h3>
<div>{this.state.name}</div>
</section>
</section>
)
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用带有webpackjs AMD的Angular2启动器.我根本没有收到任何构建错误,但是当我浏览(使用npm服务器)时,我收到一些错误:
我在构建配置中遗漏了什么吗?我需要检查什么才能使其正常工作?
预先感谢.
///错误一
长栈跟踪zone.js:106未捕获的ReferenceError:区域没有被定义(匿名功能)@长栈跟踪zone.js:106(匿名功能)@长栈跟踪zone.js:165__webpack_require__ @长重新建立了新跟踪zone.js:20NEWLINE @长栈跟踪zone.js:40232 @长栈跟踪zone.js:43__webpack_require__ @引导e8f2c957aba446c2eaa1:500 @ vendor.ts:4__webpack_require__ @引导e8f2c957aba446c2eaa1 :500 @引导e8f2c957aba446c2eaa1:93(匿名功能)@自举e8f2c957aba446c2eaa1:93个decorators.js:164(匿名功能)@ decorators.js::164未捕获反映元数据垫片是使用类decoratorscheckReflect @ decorators.js当需要166__webpack_require__ @自举e8f2c957aba446c2eaa1:50(匿名函数)@ decorators.js:2__webpack_require__ @ bootstrap e8f2c957aba446c2eaa1:50(匿名函数)@ di.js:18__webpack_require__ @ bootstrap e8f2c957aba446c2eaa1:50(匿名函数)@ browser_common.js:2__webpack_require__ @ bootstrap e8f2c957aba446c2ea a1:50(匿名函数)@ browser.js:3__webpack_require__ @ bootstrap e8f2c957aba446c2eaa1:50(匿名函数)@ boot.ts:2__webpack_require__ @ bootstrap e8f2c957aba446c2eaa1:50webpackJsonpCallback @ bootstrap e8f2c957aba446c2eaa1:21(匿名函数)@ app.bundle.js:1
//错误二
class decoratorscheckReflect @ decorators.js:164(匿名函数)@ decorators.js:166__webpack_require__ @ bootstrap e8f2c957aba446c2eaa1:50(匿名函数)@ decorators.js:2__webpack_require__ @ bootstrap e8f2c957aba446c2eaa1:50(匿名函数)@ di.js:18__webpack_require__ @ bootstrap e8f2c957aba446c2eaa1:50(匿名功能)@ browser_common.js:2__webpack_require__ @引导e8f2c957aba446c2eaa1:50(匿名功能)@ browser.js:3__webpack_require__ @引导e8f2c957aba446c2eaa1:50(匿名功能)@ boot.ts:2__webpack_require__ @引导e8f2c957aba446c2eaa1:50webpackJsonpCallback @自举e8f2c957aba446c2eaa1:21(匿名函数)@ app.bundle.js:1
//package.json
{
"name": "angular2",
"version": "1.0.0",
"description": "Angular 2",
"scripts": { …Run Code Online (Sandbox Code Playgroud)