自定义配置文件读取

配置文件应用与开放封闭原则

  配置文件用处很多,最开始使用是在数据分页数量当中。其次接触是在NHibernate当中。抽象工厂+配置文件是一个很好的应用。通过接口+配置文件,可以很方便的实现开放封闭原则。

  配置文件分为:元素,元素集合,节。

  元素: 


  
public class PersistenceElement : ConfigurationElement
{
[ConfigurationProperty(ConfigName.PropertyName, IsKey
= true , IsRequired = true )]
public string EntityClass
{
get
{
return ( string ) this [ConfigName.PropertyName];
}
set
{
this [ConfigName.PropertyName] = value;
}
}

[ConfigurationProperty(ConfigName.PropertyValue, IsRequired
= true )]
public string PersistenceClass
{
get
{
return ( string ) this [ConfigName.PropertyValue];
}
set
{
this [ConfigName.PropertyValue] = value;
}
}
}

   元素集合:


  
public class PersistenceElementCollenction : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new PersistenceElement();
}

protected override object GetElementKey(ConfigurationElement element)
{
return ((PersistenceElement)element).EntityClass;
}

public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}

protected override string ElementName
{
get { return ConfigName.PersistenceElement; }
}

public PersistenceElement this [ int index]
{
get { return (PersistenceElement) this .BaseGet(index); }
set
{
if ( this .BaseGet(index) != null )
{
this .BaseRemoveAt(index);
}
this .BaseAdd(index, value);
}
}

public new PersistenceElement this [ string interfaceShortTypeName]
{
get { return (PersistenceElement) this .BaseGet(interfaceShortTypeName); }
}

public bool ContainsKey( string keyName)
{
bool result = false ;
object [] keys = this .BaseGetAllKeys();
foreach ( object key in keys)
{
if (( string )key == keyName)
{
result
= true ;
break ;

}
}
return result;
}
}

  节:


  
public class PersistenceProperty : ConfigurationSection
{
[ConfigurationProperty(ConfigName.PersistenceElementCollection,
IsDefaultCollection
= true )]
public PersistenceElementCollenction GetElementCollection
{
get { return (PersistenceElementCollenction) base [ConfigName.PersistenceElementCollection]; }
}
}

  配置文件的书写方式如下:


  
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< configuration >
< configSections >
< section name = " PersistenceSection " type = " Configuration.PersistenceProperty, Configuration " />
</ configSections >
< PersistenceSection >
< Persistences >
< Persistence PropertyName = " A1 " PropertyValue = " A1Factory " ></ Persistence >
< Persistence PropertyName = " A2 " PropertyValue = " A2Factory " ></ Persistence >
< Persistence PropertyName = " A3 " PropertyValue = " A3Factory " ></ Persistence >
</ Persistences >
</ PersistenceSection >
</ configuration >

  总结:通过配置文件我们可以动态的加载应用程序集。从而实现开放封闭原则。

下载源代码:文件下载

转载于:https://www.cnblogs.com/yangboyu/archive/2010/06/20/1761582.html

<think>好的,我现在需要帮助用户了解如何在Vue项目中读取自定义配置文件给出方法示例。首先,我需要回顾用户提供的引用内容,尤其是引用[4]和引用[2],因为这两个引用直接涉及到Vue项目中配置文件的处理和读取。 用户的问题主要是在Vue项目中如何读取自定义配置文件,可能的需求是希望配置文件不参与打包,或者在打包后仍能方便地修改配置,而无需重新打包。根据引用[4],用户提到希望H5项目打包后仍能修改外部配置文件,比如将配置放在static目录下的config.js中,这样修改后直接生效。同时,引用[2]中的示例展示了如何在JavaScript文件中获取配置文件的属性,比如通过全局变量config.baseurl来访问配置项。 接下来,我需要整理出具体的步骤。首先,用户需要创建一个外部配置文件,例如public/config.js,在其中定义全局变量,如var CONFIG = {...}。然后在index.html中通过<script>标签引入这个配置文件,确保在Vue应用加载之前加载配置。这样,在Vue组件中就可以直接使用CONFIG对象来访问配置项了。这种方法的好处是配置文件不会被webpack打包,可以独立修改,适用于生产环境。 另外,用户可能需要区分开发环境和生产环境的配置。在开发环境中,可以使用.env文件,而生产环境则使用外部config.js。这需要配置vue.config.js,确保public目录下的文件不被处理,而是直接复制到输出目录。 此外,还需要考虑如何动态加载配置文件,特别是在生产环境中,可能需要通过异步请求来获取最新的配置,避免缓存问题。例如,在main.js中使用fetch或axios来获取config.js的内容,将其挂载到Vue实例或全局变量中,以便所有组件都可以访问。 在编写示例代码时,需要确保路径正确,比如public/config.js的位置,以及在index.html中正确引用。同时,要提醒用户注意配置文件的路径问题,避免因为路径错误导致配置加载失败。 最后,需要生成相关问题,帮助用户进一步了解相关主题,比如如何动态加载配置文件、区分环境配置,以及如何处理打包后的配置更新等。</think>### Vue项目读取自定义配置文件的方法 在Vue项目中读取自定义配置文件,通常需结合外部文件与构建工具的配置实现。以下为详细步骤及示例: --- #### 方法一:使用静态配置文件(不参与打包) **步骤1:创建配置文件** 在项目根目录的`public`文件夹下新建`config.js`,内容定义为全局变量: ```javascript // public/config.js var CONFIG = { baseUrl: "https://api.example.com", apiKey: "your_key_here" }; ``` **步骤2:引入配置文件** 在`public/index.html`中通过`<script>`标签加载配置文件: ```html <!DOCTYPE html> <html> <head> <script src="/config.js"></script> </head> <body> <div id="app"></div> </body> </html> ``` **步骤3:访问配置项** 在Vue组件中直接使用全局变量: ```javascript // 组件内使用 export default { mounted() { console.log(CONFIG.baseUrl); // 输出: https://api.example.com[^2][^4] } } ``` --- #### 方法二:通过HTTP请求动态加载 **步骤1:配置文件放置** 将`config.json`放在服务器静态资源目录(如Nginx的`/static`文件夹): ```json { "themeColor": "#1890ff", "maxPageSize": 20 } ``` **步骤2:请求配置文件** 在应用初始化时通过异步请求获取: ```javascript // main.js import axios from 'axios'; axios.get('/static/config.json') .then(response => { Vue.prototype.$config = response.data; new Vue({ render: h => h(App) }).$mount('#app'); }) .catch(error => console.error('加载配置失败:', error)); ``` **步骤3:组件内调用** ```javascript this.$config.themeColor // 获取配置值 ``` --- #### 配置打包排除(vue.config.js) 确保文件不被Webpack处理: ```javascript // vue.config.js module.exports = { chainWebpack: config => { config.module.rule('js').exclude.add(/config\.js$/); } }; ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值