我在用匕首插入字符串时遇到问题
这是我的实现
@Singleton
@Component(
modules = [AndroidInjectionModule::class,
ActivityBuilder::class,
ViewModelModule::class,
NetModule::class,
AppModule::class]
)
interface AppComponent : AndroidInjector<DaggerApplication> {
fun inject(theDApplication: TFTScreenApplication)
override fun inject(instance: DaggerApplication)
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): Builder
@BindsInstance
@Named(Constants.API_URL_KEY)
fun apiUrl(apiUrl: String): Builder
@BindsInstance
@Named(Constants.SOCKET_URL_KEY)
fun socketUrl(socketUrl: String): Builder
fun build(): AppComponent
}
}
然后使用网络模块
@Module
abstract class NetModule {
@Binds
@Named(Constants.API_URL_KEY)
abstract fun provideApiUrl(apiUrl: String): String
@Binds
@Named(Constants.SOCKET_URL_KEY)
abstract fun provideSocketUrl(socketUrl: String): String
@Module
companion object {
@Provides
@Reusable
@JvmStatic
fun providesOkHttpClient(): OkHttpClient {
return OkHttpClient.Builder()
.build()
}
@Provides
@Reusable
@JvmStatic
fun providesGson(): Gson {
val gsonBuilder = GsonBuilder()
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
return gsonBuilder.create()
}
@Provides
@Reusable
@JvmStatic
fun providesRetrofit(
@Named(Constants.API_URL_KEY) apiUrl: String, gson: Gson
): Retrofit {
return Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(apiUrl)
.build()
}
@Provides
@Reusable
@JvmStatic
fun providesBackOffStrategy(): BackoffStrategy {
return ExponentialWithJitterBackoffStrategy(5000, 5000)
}
@Provides
@Reusable
@JvmStatic
fun providesLifeCycle(application: Application): Lifecycle {
return AndroidLifecycle.ofApplicationForeground(application)
}
@Provides
@Reusable
@JvmStatic
fun providesScarlet(
@Named(Constants.SOCKET_URL_KEY) socketUrl: String, okHttpClient: OkHttpClient,
backoffStrategy: BackoffStrategy,
lifecycle: Lifecycle
): Scarlet {
return Scarlet.Builder()
.webSocketFactory(okHttpClient.newWebSocketFactory(socketUrl))
.addMessageAdapterFactory(MoshiMessageAdapter.Factory())
.addStreamAdapterFactory(RxJava2StreamAdapterFactory())
.backoffStrategy(backoffStrategy)
.lifecycle(lifecycle)
.build()
}
@Provides
@Reusable
@JvmStatic
fun providesCoinSocket(scarlet: Scarlet): SocketService {
return scarlet.create(SocketService::class.java)
}
@Provides
@Reusable
@JvmStatic
fun providesPISAPIs(retrofit: Retrofit): PISAPIs {
return retrofit.create(PISAPIs::class.java)
}
}
错误是
错误:[Dagger / DuplicateBindings] java.lang.String被绑定多个 时间:公共抽象接口AppComponent扩展 dagger.android.AndroidInjector { ^ @ org.jetbrains.annotations.NotNull @Named(“ API_URL”)@BindsInstance com.example.tftscreen.common.di.component.AppComponent.Builder com.example.tftscreen.common.di.component.AppComponent.Builder.apiUrl(String) @ org.jetbrains.annotations.NotNull @Named(“ SOCKET_URL”)@BindsInstance com.example.tftscreen.common.di.component.AppComponent.Builder com.example.tftscreen.common.di.component.AppComponent.Builder.socketUrl(String) java.lang.String被注入 com.example.tftscreen.common.di.module.NetModule.provideSocketUrl(socketUrl) @ javax.inject.Named(“ SOCKET_URL”)java.lang.String被注入 com.example.tftscreen.common.di.module.NetModule.providesScarlet(socketUrl, …) com.tinder.scarlet.Scarlet被注入 com.example.tftscreen.common.di.module.NetModule.providesCoinSocket(scarlet) com.example.tftscreen.pis.SocketService注入在 com.example.tftscreen.pis.data.PISRemoteRepository(socketService,…) com.example.tftscreen.pis.data.PISRemoteRepository被注入到 com.example.tftscreen.pis.PISViewModel(pisRemoteRepository) com.example.tftscreen.pis.PISViewModel被注入到 com.example.tftscreen.common.di.module.ViewModelModule.bindPISViewModel(pisViewModel) java.util.Map,javax.inject.Provider> 在注入 com.example.tftscreen.common.presentationLayer.ViewModelFactory(创建者) com.example.tftscreen.common.presentationLayer.ViewModelFactory注入在 com.example.tftscreen.common.di.module.ViewModelModule.provideViewModelFactory(viewModelFactory) androidx.lifecycle.ViewModelProvider.Factory被注入 com.example.tftscreen.pis.PISActivity.viewModelFactory com.example.tftscreen.pis.PISActivity在以下位置注入 dagger.android.AndroidInjector.inject(T)[com.example.tftscreen.common.di.component.AppComponent→ com.example.tftscreen.common.di.module.ActivityBuilder_BindMainActivity.PISActivitySubcomponent] 也可以在以下位置提出要求: com.example.tftscreen.common.di.module.NetModule.provideApiUrl(apiUrl)
答案 0 :(得分:0)
class AdditionalCharge: Codable {
var ItemDescription = ""
var ItemValue:String = ""
}
,当您需要在运行时间 中将依赖关系引入对象图中时,最有用。
<v-data-table
:headers="headers"
:items="myItems"
:search="search"
no-results-text='LMAO NO DATA'
rows-per-page-text='Data'
:rows-per-page-items="[5, 50, 100, {text:'EVERYTHING', value: -1}] "
class="elevation-3"
>
用于需要将接口绑定到实现 时,dagger可以为您构造具体的实现(通过@BindsInstance
构造函数)
您的@Binds
和@Inject
似乎不符合以下两个条件之一,所以...
如果确实在编译时 可以使用它们,并且您想让匕首提供它们,那么最简单的方法是在模块中添加以下内容:
Constants.SOCKET_URL_KEY
并删除:
Constants.API_URL_KEY
在两种情况下(在运行时或编译时可用),您都需要删除此内容:
@Provides
@JvmStatic
@Named(Constants.API_URL_KEY)
fun providesApiUrlKey(): String {
return "YOUR_API_KEY"
}