我在模块中使用angular-cli的环境变量.当我在另一个项目中导入我的模块时,是否可以在编译和运行时使用我的项目的环境变量?
我的模块
myModule/
src/
/app
my.service.ts
/environments
environment.prod.ts
environment.ts
app.module.ts
etc.
Run Code Online (Sandbox Code Playgroud)
我的模块是my.service.ts
import { environment } from './environments/environment';
@Injectable()
export class MyService {
private title = environment.title;
showTitle(): string {
return this.title;
}
etc.
}
Run Code Online (Sandbox Code Playgroud)
我的模块的环境.ts
export const environment = {
production: false,
title: 'My Module in dev mode'
}
Run Code Online (Sandbox Code Playgroud)
我的模块的environment.prod.ts
export const environment = {
production: true,
title: 'My Module in prod mode'
}
Run Code Online (Sandbox Code Playgroud)
我的项目
myProject/
src/
/app
app.component.ts
/environments
environment.prod.ts
environment.ts
app.module.ts
etc.
Run Code Online (Sandbox Code Playgroud)
我的项目是AppComponent
Component({
selector: 'app-root',
template: …Run Code Online (Sandbox Code Playgroud) 对于任何好的反应,我试图将我的组件注入服务,但我获得了一个新的实例.我用这段代码重现了我的问题:
该组件将在h1以下位置显示实例编号:
@Component({
selector: 'my-component',
template: '<h1>Instance number {{count}}</h1>'
})
export class MyComponent {
private static i = 0; /* <-- counts the myComponent's instances here */
private _count: number = i++;
get count(): number {return this._count}
}
Run Code Online (Sandbox Code Playgroud)
服务将在控制台中记录实例编号:
@Injectable()
export class MyService {
constructor(myComponent: MyComponent) {
console.log("Instance number " + myComponent.count);
}
}
Run Code Online (Sandbox Code Playgroud)
主要组件将在视图和服务中注入组件:
@Component({
selector: 'app-root',
template: '<my-component></my-component>',
})
export class AppComponent {
constructor(service: MyService) {
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用angular-cli,我的app.module.ts看起来:
@NgModule({
declarations: [
AppComponent,
MyComponent
],
imports: …Run Code Online (Sandbox Code Playgroud) 我曾经使用包含发布阶段的 DSL 管道将我的 NPM 项目发布到 Nexus,该管道包含以下步骤:
stage ('Publish') {
nodejs(nodeJSInstallationName: 'Node LTS', configId: '123456ab-1234-abcd-1234-f123d45e6789') {
sh 'npm publish'
}
}
Run Code Online (Sandbox Code Playgroud)
我的 Jenkins 上安装了一个名为“Node LTS”的 NodeJS,并且有一个带有此 configId 的 npmrc 配置文件。
现在我想将这个阶段导出到一个常规的 SharedLib 中。根据声明性管道文档和这个nodejs-plugin问题,我可以这样写:
stage('Publish') {
tools {
nodejs 'Node LTS'
}
steps {
sh 'npm publish'
}
}
Run Code Online (Sandbox Code Playgroud)
但这不会设置当前在我的 npmrc 配置文件中的身份验证配置:
registry=http://my-nexus/repository/npm-private/
_auth="some=base=64=credential=="
always-auth=true
Run Code Online (Sandbox Code Playgroud)
有什么想法可以使用声明性语法检索此配置并防止出现此错误消息吗?
npm ERR! code ENEEDAUTH
npm ERR! need auth auth required for publishing
npm ERR! need auth You need to authorize this machine …Run Code Online (Sandbox Code Playgroud) node.js jenkins jenkins-pipeline jenkins-declarative-pipeline