我想在显示 SVG 图像的网站上实现缩放功能。
我看到了这个库github.com/ariutta/svg-pan-zoom
,它提供了我需要的确切功能,
但是我似乎无法让它与 angular2 一起使用,无法访问窗口。
经过一些研究,我想我必须将窗口填充到 svg-pan-zoom 的 webpack 导出中。也许我不是在寻找正确的东西,但我认为这是非常惊人的,有这么多的工作需要完成,只是为了导入一个 3rd 方 javascript。我能找到的最好的线索是:https
://github.com/ariutta/svg-pan-zoom/issues/207编辑:见答案。
我使用了这个 Angular 2 aspnet 核心启动项目: https ://damienbod.com/2017/01/01/building-production-ready-angular-apps-with-visual-studio-and-asp-net-core/
编辑:实际上是这个 https://github.com/MarkPieszak/aspnetcore-angular2-universal但是分支在我发布这篇文章时得到了更新,这让我感到困惑
我这里有这个服务,
svg-pan-zoom.service.ts
import { Injectable } from '@angular/core'
import { isBrowser } from 'angular2-universal';
import * as svgPanZoom from 'svg-pan-zoom';
@Injectable()
export class SvgPanZoomService {
getPanZoom(element: any) {
if (isBrowser) {
svgPanZoom(element);
}
}
}
这里称为 map.component.ts
import { Component, AfterViewInit } from '@angular/core';
import { SvgPanZoomService } from '../../injectables/svg-pan-zoom.service';
import { isBrowser } from 'angular2-universal';
@Component({
selector: 'map-full',
template: require('./map.component.html'),
styles: [require('./map.component.css')]
})
export class MapComponent implements AfterViewInit {
constructor(private svgZoom: SvgPanZoomService) {
if (isBrowser) {
this.svgZoom.getPanZoom('#evSvgMap');
}
}
}
我的 app.module 中引用了 SvgPanZoomService 服务
我的 package.json 中引用了 Svg-pan-zoom 库,
最后,我的构建给了我 2 个 js 文件,main-client.js 和 vendor.js
我可以浏览 main-client.js 并看到它引用了 svg-pan-zoom,
当我的页面加载时它出现在我的浏览器源中
但是当谈到加载它应该做的事情的部分时,我得到了这个错误。
An unhandled exception occurred while processing the request.
Exception: Call to Node module failed with error:
Prerendering failed because of error: ReferenceError: window is not defined
at D:\[mystuff]\node_modules\svg-pan-zoom\dist\svg-pan-zoom.js:1493:8
现在我读到我不打算从组件访问窗口,但我对 lib 调用的内容没有发言权,我在这里读到某处添加(isBrowser)
应该验证我没有调用这个服务器端(为什么我希望服务器缩放这个你是对的吗?)
这是我的 webpack.config.js
var isDevBuild = process.argv.indexOf('--env.prod') < 0;
var path = require('path');
var webpack = require('webpack');
var nodeExternals = require('webpack-node-externals');
var merge = require('webpack-merge');
var allFilenamesExceptJavaScript = /\.(?!js(\?|$))([^.]+(\?|$))/;
// Configuration in common to both client-side and server-side bundles
var sharedConfig = {
resolve: { extensions: [ '', '.js', '.ts' ] },
output: {
filename: '[name].js',
publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
},
module: {
loaders: [
{ test: /\.ts$/, include: /ClientApp/, loader: 'ts', query: { silent: true } },
{ test: /\.html$/, loader: 'raw' },
{ test: /\.css$/, loader: 'to-string!css' },
{ test: /\.(png|jpg|jpeg|gif|svg)$/, loader: 'url', query: { limit: 25000 } }
]
}
};
// Configuration for client-side bundle suitable for running in browsers
var clientBundleConfig = merge(sharedConfig, {
entry: {
'main-client': './ClientApp/boot-client.ts'
},
output: { path: path.join(__dirname, './wwwroot/dist') },
devtool: isDevBuild ? 'inline-source-map' : null,
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./wwwroot/dist/vendor-manifest.json')
})
].concat(isDevBuild ? [] : [
// Plugins that apply in production builds only
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin()
])
});
// Configuration for server-side (prerendering) bundle suitable for running in Node
var serverBundleConfig = merge(sharedConfig, {
entry: { 'main-server': './ClientApp/boot-server.ts' },
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, './ClientApp/dist')
},
target: 'node',
devtool: 'inline-source-map',
externals: [nodeExternals({ whitelist: [allFilenamesExceptJavaScript] })] // Don't bundle .js files from node_modules
});
module.exports = [clientBundleConfig, serverBundleConfig];
该应用程序然后加载另一个 webpack.config.vendor.js
var isDevBuild = process.argv.indexOf('--env.prod') < 0;
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var extractCSS = new ExtractTextPlugin('vendor.css');
module.exports = {
resolve: {
extensions: [ '', '.js' ]
},
module: {
loaders: [
{ test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, loader: 'url-loader?limit=100000' },
{ test: /\.css(\?|$)/, loader: extractCSS.extract(['css']) }
{ test: require("svg-pan-zoom"),
loader: "imports-loader?window=>window./svg-pan-zoom.js"} // wild try
]
},
entry: {
vendor: [
'@angular/common',
'@angular/compiler',
'@angular/core',
'@angular/http',
'@angular/platform-browser',
'@angular/platform-browser-dynamic',
'@angular/router',
'@angular/platform-server',
'angular2-universal',
'angular2-universal-polyfills',
'bootstrap',
'bootstrap/dist/css/bootstrap.css',
'es6-shim',
'es6-promise',
'jquery',
'zone.js',
'svg-pan-zoom' //i added this, no clue if it's relevant.
//EDIT :Turns out it was important, very much so.
]
},
output: {
path: path.join(__dirname, 'wwwroot', 'dist'),
filename: '[name].js',
library: '[name]_[hash]',
},
plugins: [
extractCSS,
new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery'}), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable)
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.DllPlugin({
path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'),
name: '[name]_[hash]'
})
].concat(isDevBuild ? [] : [
new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } })
])
};
我还有 2 个与 webpack.config.js 相关的文件 boot-client 和 boot-server,它们是用于打包服务器和客户端文件的说明。
谢谢。