0

Trying to better understand the @component selector and template to path my routing to an html file. I am getting errors when trying to simply point to the html file even though it seems I have what I need in my index.ts files. The errors I am getting are:

enter image description here

Looking at the involved files, I am not sure what is wrong. Here is my user.component.ts:

import {Component} from '@angular/core';

@Component({
  selector: 'users',
  template: require('./components/userMgmt/inviteUser.html')
})
export class userComponent {}

Then my index.ts from the root of app:

import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import 'jquery';
import 'bootstrap';
import 'font-awesome/css/font-awesome.css';

import {routing, RootComponent} from './routes';
import {AppComponent} from './containers/app';
import {HeaderComponent} from './components/header';
import {userComponent} from './components/userMgmt/user.component';

@NgModule({
  imports: [
    BrowserModule,
    routing
  ],
  declarations: [
    RootComponent,
    AppComponent,
    HeaderComponent,
    userComponent
  ],
  bootstrap: [RootComponent]
})
export class AppModule {}

And finally, my index.ts from the root of my /src:

import 'core-js/client/shim';
import 'zone.js/dist/zone';

import '@angular/common';
import 'rxjs';

import './index.scss';

import {enableProdMode} from '@angular/core';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app';


declare var process: any;
if (process.env.NODE_ENV === 'production') {
  enableProdMode();
} else {
  Error['stackTraceLimit'] = Infinity; // tslint:disable-line:no-string-literal
  require('zone.js/dist/long-stack-trace-zone'); // tslint:disable-line:no-var-requires
}

platformBrowserDynamic().bootstrapModule(AppModule);

Where I am going wrong on this? I don't see any issues. Am I not using template correctly? Seems I should be able to inject html or add a path to the html file, no?

Per the first answer, it fixed part of it, but not understanding why now it doesn't understand the path. The console no longer out puts an error, but the page does not load in the browser. Here is the error:

enter image description here

And here is a screenshot of my file structure:enter image description here

2 Answers 2

1

Looking at your component folder and code. I feel that the path to the component html is incorrect. Try this code and see.

import {Component} from '@angular/core';

@Component({
  selector: 'users',
  templateUrl: './inviteUser.html'
})
export class userComponent {}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, that helped, but rather than the other error in my console, now dev tools says it cannot find. See last 2 screenshots in updated question to see what I am referring to.
So it seems if I add as an absolute URL, it resolves fine. As such, this: templateUrl: 'app/components/userMgmt/inviteUser.html' work, but just a relative URL does not. Ideas?
I'm looking at possible scenarios. Which version of Angular are you working on? Did u you use angular-cli to setup the project? I strongly feel, something wrong with configuration but not sure.
Try adding "moduleId: module.id," to your component decorator, but not necessary with the latest version of angular. Please refer to the change log: angular.io/guide/change-log
Using 2 currently, but am going to move it to 4 shortly. So I should just accept the absolute paths for now and circle back to this later.
1

Add moduleID in @component

 @Component({
      selector: 'users',
      moduleId: module.id,
      templateUrl: './inviteUser.html'
 })

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.