Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRA依赖注入不生效? #242

Closed
gitHber opened this issue May 26, 2020 · 8 comments
Closed

CRA依赖注入不生效? #242

gitHber opened this issue May 26, 2020 · 8 comments
Assignees
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed

Comments

@gitHber
Copy link

gitHber commented May 26, 2020

create-react-app使用ts,依赖注入不生效,添加 babel-plugin-transform-typescript-metadata 也没用
App.js

import "reflect-metadata";
import React from "react";
import { initDevtool } from "@sigi/devtool";
import { TestModule } from "./modules/test.module";
import { useModule } from "@sigi/react";

initDevtool();
function App() {
  const [state, dispatcher] = useModule(TestModule);
  return (
    <div className="App">
      {state.count}
      <button onClick={() => dispatcher.add()}>add</button>
    </div>
  );
}

export default App;

a.service.ts

import { Injectable } from "@sigi/di";

@Injectable()
export default class AService {
  do() {
    console.log("do something");
  }
}

test.module.ts

import {
  Module,
  EffectModule,
  ImmerReducer,
} from "@sigi/core";
import { Observable } from "rxjs";

import AService from "../services/a.service";

interface State {
  count: number;
}

@Module("Test")
export class TestModule extends EffectModule<State> {
  defaultState = { count: 0 };
  constructor(private readonly a: AService) {
    super();
  }
  @ImmerReducer()
  add(state: State) {
    console.log(this);
    state.count += 1;
  }
}

babel.config.js

module.exports = {
  plugins: [
    "babel-plugin-transform-typescript-metadata",
    ["@babel/plugin-proposal-decorators", { legacy: true }],
    ["@babel/plugin-proposal-class-properties", { loose: true }],
  ],
};
@Brooooooklyn
Copy link
Member

@gitHber 你的 tsconfig 中

"emitDecoratorMetadata": true,
"experimentalDecorators": true,

两个选项开了吗

@gitHber
Copy link
Author

gitHber commented May 26, 2020

@gitHber 你的 tsconfig 中

"emitDecoratorMetadata": true,
"experimentalDecorators": true,

两个选项开了吗

开了的

@Brooooooklyn
Copy link
Member

将你的 initDevtool 移到 ReactDOM.render 后执行

@gitHber
Copy link
Author

gitHber commented May 26, 2020

没有变化

@Brooooooklyn
Copy link
Member

CRA 默认不允许修改任何配置,你需要先使用 react-app-rewired 重写 CRA 的 babel config storybookjs/storybook#6479 (comment)

或者使用 customize-cra: rbuckton/reflect-metadata#104 (comment)

@Brooooooklyn Brooooooklyn added documentation Improvements or additions to documentation good first issue Good for newcomers help wanted Extra attention is needed and removed good first issue Good for newcomers labels May 27, 2020
@gitHber
Copy link
Author

gitHber commented May 27, 2020

你能给个例子吗?我都试过了,还是不生效,目前我只能手动注入

@Brooooooklyn
Copy link
Member

@gitHber
Copy link
Author

gitHber commented May 30, 2020

@gitHber https://github.com/sigi-framework/cra-example
thx, 好像是我用的是babel.config.js,而不是.babelrc,所以没能识别
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants