Skip to content

Instantly share code, notes, and snippets.

@goughjo02
goughjo02 / typescript-example-1.ts
Last active February 25, 2019 12:41
With Typescript one can define Types, define what 'type' is returned from a method, of or define which 'type' an argument is.
// Here is a Person type
type Person {
name: string;
age: number;
}
// This function accepts a Person object and returns their name (a string)
function getName(person: Person): string {
return person.name;
}
// This function takes an arguement array of people and returns their combined age (a number)
import { SelectionModel } from "@angular/cdk/collections";
@Component(...metaData)
export class SelectionExample {
peopleSelection = new SelectionModel<Person>(true /* multiple */);
select(person: Person): void {
this.peopleSelection.select(person);
return;
}
@Injectable()
export class MyInjectableService {}
// Define the Module
// Inject the service
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
@NgModule({
imports: [BrowserModule],
// Module file
// Injecting the service
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
@NgModule({
imports: [BrowserModule],
declarations: [AppComponent],
// See here
// Component file
// Where the service is injected
import { SelectionModel } from "@angular/cdk/collections";
@Component(...metaData)
export class AppComponent implements OnInit {
constructor(peopleSelection: PeopleSelectionService) {}
ngOnInit(): void {
console.log(
@Injectable({
providedIn: "root"
})
export class SingletonRootService {}
import { SelectionModel } from "@angular/cdk/collections";
@Component(...metaData)
export class AppComponent implements OnInit {
constructor(peopleSelection: PeopleSelectionService) {}
ngOnInit(): void {
this.peopleSelection.selection.changed.subscribe(e => {
console.log(`I have received a value ${e}`);
});
}
import(pathtofile).then(e => {
console.log("module loaded!");
});
import React from "react";
export default class LazilyLoadedCustomFeature extends Component {
state = {
content: null
};
componentDidMount() {
import(pathtofile).then(e => {
this.setState({ content: e });
});
import CustomFeature from “path-to-file”;