Error message

declare let numeral:any;

@Component({     
      //moduleId: module.id,     
      selector: 'app-dashboard',   
      templateUrl: './dashboard.component.html',     
      styleUrls:  hboard.component.css' 
})

मैं Angular2 + Webpack3 का उपयोग कर रहा हूं।

यहाँ मेरा webpack.config.ts और tsconfig.json है।

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');


function webpackConfig(options: EnvOptions = {}): WebpackConfig {

  return {
      cache: false,

      entry: {
          polyfills: './src/polyfills',
          vendor:       './src/vendor',
          main:           './src/main'
      },

      output: {
          path: path.join(__dirname, '/public'),
          filename: '[name].bundle.js',
          sourceMapFilename: '[name].map',
          chunkFilename: '[id].chunk.js'
      },

      module: {
          loaders: [


               { test:/\.ts$/,loaders:['awesome-typescript-loader','angular2-template-loader']},
               { test: /\.json$/,loader:'json-loader'},
               { test: /\.html$/, loader:'html-loader'},
               { test: /\.(css|scss)$/, loaders:['to-string-loader','css-loader','sass-loader'] },
               { test: /\.json$/, loader: 'raw-loader' },
               { test: /\.(png|woff|woff2|eot|ttf|svg)(\?v=\d+\.\d+\.\d+)?$/, loader: 'url-loader?limit=100000' }
          ]
      },

      plugins: [
              new HtmlWebpackPlugin({
                      template: './src/index.html',
                      filename: './index.html',
                      inject: 'body'
              }),

              new webpack.LoaderOptionsPlugin({
                  debug: true,
                  options: {
                      context: __dirname,   
                      output: { path :   './' }, //This has to be './' and not your output folder.
                      sassLoader: {
                          includePaths: [path.resolve(__dirname, 'src', 'scss')]
                      }
                  }
              }),
              new webpack.HotModuleReplacementPlugin(),
              new webpack.optimize.CommonsChunkPlugin({
                      name: ['main', 'vendor', 'polyfills'],
                      minChunks: Infinity
              }),
              new webpack.ContextReplacementPlugin(
                      /@angular(\\|\/)core/,
                      path.join(__dirname, 'src'),
                      {}
              ),
              new webpack.DefinePlugin({
                      ENV: JSON.stringify(options.ENV),
                      HMR: options.HMR
              })
      ],

      devtool: 'cheap-eval-source-map',
      resolve: {
          modules: [ path.join(__dirname, 'node_modules/'), path.join(__dirname, 'src/')],
          extensions: ['.ts', '.js', '.json', '.html']
      },

      devServer: {
          contentBase: path.join(__dirname, '/public'),
          port: 4200,
          historyApiFallback: true
      }
  };
}

// Export
module.exports = logOptions(webpackConfig);

function logOptions(fn) {
  return function(options: EnvOptions = {}) {
      console.log('Env Options: ', JSON.stringify(options, null, 2));
      return fn(options);
  };
}

// Types
type Entry = Array<string> | Object;

type Output = Array<string> | {
  path: string,
  filename: string
};

type EnvOptions = any;

interface WebpackConfig {
  cache?: boolean;
  devtool?: string;
  entry: Entry;
  output: any;
  module?: {
      loaders?: Array<any>
  },
  plugins?: Array<any>;
  resolve?: {
      extensions?: Array<string>;
      modules: Array<string>;
  },
  devServer?: {
      contentBase?: string;
      port?: number;
      historyApiFallback?: boolean;
      hot?: boolean;
  }
}

Tsconfig.json फ़ाइल

 {
  "compileOnSave": false,
  "compilerOptions": {
      "module" : "commonjs",
      "moduleResolution": "node",
      "declaration": false,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "noEmitOnError": true,
      "noImplicitAny": false,
      "removeComments": false,
      "outDir": "./public",
      "sourceMap": false,
      "target": "es5",
      "lib": [
          "es2015",
          "dom"
      ],
      "baseUrl": ".",
      "paths": {
          "@angular/*": ["node_modules/@angular/*"]
      },
      "typeRoots": [
          "node_modules/@types"
      ],
      "types": [
          "node"
      ]
  },
  "include" : [
      "src/app/**/*"
  ],
  "exclude": [
      "node_modules",
      "public",
      "src/app/**/*.spec.ts",
      "src/app/**/*.e2e.ts"
  ],
  "files": [
      "main.ts",
      "./src/app/**/*.ts"
  ]
}
0
Fury 1 नवम्बर 2017, 17:01

1 उत्तर

यह डिज़ाइन द्वारा है, आप केवल टेम्पलेट से घटक के गुणों तक पहुंच सकते हैं।

declare let numeral:any;

numeral को घटक से बाहर घोषित कर दिया गया है, इसलिए यह पहुंच योग्य नहीं है।

संपादित करें: यह काम करना चाहिए

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

import numeral from 'numeral';

@Component({
  selector: 'my-app',
  template: `{{myNumber}}`,
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  get myNumber () {
     numeral(1000).format('0,0'); 
  }
}

यदि आप अपने टेम्पलेट में अंक () का उपयोग करना चाहते हैं तो आपको एक पाइप बनाना चाहिए

import numeral from 'numeral';
@Pipe({
name:'numeral'
})
@Injectable()
export class NumeralPipe implements PipeTransform{
    transform(myNumber:any):any{ 
      numeral(1000).format('0,0'); 
    }; 
} 

और इसे इस तरह इस्तेमाल करें

<div>{{ myNumber | numeral }}</div>
2
Kld 1 नवम्बर 2017, 18:12